soc-core.c 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461
  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. #ifdef CONFIG_DEBUG_FS
  40. static struct dentry *debugfs_root;
  41. #endif
  42. static DEFINE_MUTEX(client_mutex);
  43. static LIST_HEAD(card_list);
  44. static LIST_HEAD(dai_list);
  45. static LIST_HEAD(platform_list);
  46. static LIST_HEAD(codec_list);
  47. static int snd_soc_register_card(struct snd_soc_card *card);
  48. static int snd_soc_unregister_card(struct snd_soc_card *card);
  49. /*
  50. * This is a timeout to do a DAPM powerdown after a stream is closed().
  51. * It can be used to eliminate pops between different playback streams, e.g.
  52. * between two audio tracks.
  53. */
  54. static int pmdown_time = 5000;
  55. module_param(pmdown_time, int, 0);
  56. MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
  57. /*
  58. * This function forces any delayed work to be queued and run.
  59. */
  60. static int run_delayed_work(struct delayed_work *dwork)
  61. {
  62. int ret;
  63. /* cancel any work waiting to be queued. */
  64. ret = cancel_delayed_work(dwork);
  65. /* if there was any work waiting then we run it now and
  66. * wait for it's completion */
  67. if (ret) {
  68. schedule_delayed_work(dwork, 0);
  69. flush_scheduled_work();
  70. }
  71. return ret;
  72. }
  73. #ifdef CONFIG_SND_SOC_AC97_BUS
  74. /* unregister ac97 codec */
  75. static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
  76. {
  77. if (codec->ac97->dev.bus)
  78. device_unregister(&codec->ac97->dev);
  79. return 0;
  80. }
  81. /* stop no dev release warning */
  82. static void soc_ac97_device_release(struct device *dev){}
  83. /* register ac97 codec to bus */
  84. static int soc_ac97_dev_register(struct snd_soc_codec *codec)
  85. {
  86. int err;
  87. codec->ac97->dev.bus = &ac97_bus_type;
  88. codec->ac97->dev.parent = codec->card->dev;
  89. codec->ac97->dev.release = soc_ac97_device_release;
  90. dev_set_name(&codec->ac97->dev, "%d-%d:%s",
  91. codec->card->number, 0, codec->name);
  92. err = device_register(&codec->ac97->dev);
  93. if (err < 0) {
  94. snd_printk(KERN_ERR "Can't register ac97 bus\n");
  95. codec->ac97->dev.bus = NULL;
  96. return err;
  97. }
  98. return 0;
  99. }
  100. #endif
  101. static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream)
  102. {
  103. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  104. struct snd_soc_device *socdev = rtd->socdev;
  105. struct snd_soc_card *card = socdev->card;
  106. struct snd_soc_dai_link *machine = rtd->dai;
  107. struct snd_soc_dai *cpu_dai = machine->cpu_dai;
  108. struct snd_soc_dai *codec_dai = machine->codec_dai;
  109. int ret;
  110. if (codec_dai->symmetric_rates || cpu_dai->symmetric_rates ||
  111. machine->symmetric_rates) {
  112. dev_dbg(card->dev, "Symmetry forces %dHz rate\n",
  113. machine->rate);
  114. ret = snd_pcm_hw_constraint_minmax(substream->runtime,
  115. SNDRV_PCM_HW_PARAM_RATE,
  116. machine->rate,
  117. machine->rate);
  118. if (ret < 0) {
  119. dev_err(card->dev,
  120. "Unable to apply rate symmetry constraint: %d\n", ret);
  121. return ret;
  122. }
  123. }
  124. return 0;
  125. }
  126. /*
  127. * Called by ALSA when a PCM substream is opened, the runtime->hw record is
  128. * then initialized and any private data can be allocated. This also calls
  129. * startup for the cpu DAI, platform, machine and codec DAI.
  130. */
  131. static int soc_pcm_open(struct snd_pcm_substream *substream)
  132. {
  133. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  134. struct snd_soc_device *socdev = rtd->socdev;
  135. struct snd_soc_card *card = socdev->card;
  136. struct snd_pcm_runtime *runtime = substream->runtime;
  137. struct snd_soc_dai_link *machine = rtd->dai;
  138. struct snd_soc_platform *platform = card->platform;
  139. struct snd_soc_dai *cpu_dai = machine->cpu_dai;
  140. struct snd_soc_dai *codec_dai = machine->codec_dai;
  141. int ret = 0;
  142. mutex_lock(&pcm_mutex);
  143. /* startup the audio subsystem */
  144. if (cpu_dai->ops->startup) {
  145. ret = cpu_dai->ops->startup(substream, cpu_dai);
  146. if (ret < 0) {
  147. printk(KERN_ERR "asoc: can't open interface %s\n",
  148. cpu_dai->name);
  149. goto out;
  150. }
  151. }
  152. if (platform->pcm_ops->open) {
  153. ret = platform->pcm_ops->open(substream);
  154. if (ret < 0) {
  155. printk(KERN_ERR "asoc: can't open platform %s\n", platform->name);
  156. goto platform_err;
  157. }
  158. }
  159. if (codec_dai->ops->startup) {
  160. ret = codec_dai->ops->startup(substream, codec_dai);
  161. if (ret < 0) {
  162. printk(KERN_ERR "asoc: can't open codec %s\n",
  163. codec_dai->name);
  164. goto codec_dai_err;
  165. }
  166. }
  167. if (machine->ops && machine->ops->startup) {
  168. ret = machine->ops->startup(substream);
  169. if (ret < 0) {
  170. printk(KERN_ERR "asoc: %s startup failed\n", machine->name);
  171. goto machine_err;
  172. }
  173. }
  174. /* Check that the codec and cpu DAI's are compatible */
  175. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  176. runtime->hw.rate_min =
  177. max(codec_dai->playback.rate_min,
  178. cpu_dai->playback.rate_min);
  179. runtime->hw.rate_max =
  180. min(codec_dai->playback.rate_max,
  181. cpu_dai->playback.rate_max);
  182. runtime->hw.channels_min =
  183. max(codec_dai->playback.channels_min,
  184. cpu_dai->playback.channels_min);
  185. runtime->hw.channels_max =
  186. min(codec_dai->playback.channels_max,
  187. cpu_dai->playback.channels_max);
  188. runtime->hw.formats =
  189. codec_dai->playback.formats & cpu_dai->playback.formats;
  190. runtime->hw.rates =
  191. codec_dai->playback.rates & cpu_dai->playback.rates;
  192. } else {
  193. runtime->hw.rate_min =
  194. max(codec_dai->capture.rate_min,
  195. cpu_dai->capture.rate_min);
  196. runtime->hw.rate_max =
  197. min(codec_dai->capture.rate_max,
  198. cpu_dai->capture.rate_max);
  199. runtime->hw.channels_min =
  200. max(codec_dai->capture.channels_min,
  201. cpu_dai->capture.channels_min);
  202. runtime->hw.channels_max =
  203. min(codec_dai->capture.channels_max,
  204. cpu_dai->capture.channels_max);
  205. runtime->hw.formats =
  206. codec_dai->capture.formats & cpu_dai->capture.formats;
  207. runtime->hw.rates =
  208. codec_dai->capture.rates & cpu_dai->capture.rates;
  209. }
  210. snd_pcm_limit_hw_rates(runtime);
  211. if (!runtime->hw.rates) {
  212. printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
  213. codec_dai->name, cpu_dai->name);
  214. goto machine_err;
  215. }
  216. if (!runtime->hw.formats) {
  217. printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
  218. codec_dai->name, cpu_dai->name);
  219. goto machine_err;
  220. }
  221. if (!runtime->hw.channels_min || !runtime->hw.channels_max) {
  222. printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
  223. codec_dai->name, cpu_dai->name);
  224. goto machine_err;
  225. }
  226. /* Symmetry only applies if we've already got an active stream. */
  227. if (cpu_dai->active || codec_dai->active) {
  228. ret = soc_pcm_apply_symmetry(substream);
  229. if (ret != 0)
  230. goto machine_err;
  231. }
  232. pr_debug("asoc: %s <-> %s info:\n", codec_dai->name, cpu_dai->name);
  233. pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates);
  234. pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
  235. runtime->hw.channels_max);
  236. pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
  237. runtime->hw.rate_max);
  238. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  239. cpu_dai->playback.active = codec_dai->playback.active = 1;
  240. else
  241. cpu_dai->capture.active = codec_dai->capture.active = 1;
  242. cpu_dai->active = codec_dai->active = 1;
  243. cpu_dai->runtime = runtime;
  244. card->codec->active++;
  245. mutex_unlock(&pcm_mutex);
  246. return 0;
  247. machine_err:
  248. if (machine->ops && machine->ops->shutdown)
  249. machine->ops->shutdown(substream);
  250. codec_dai_err:
  251. if (platform->pcm_ops->close)
  252. platform->pcm_ops->close(substream);
  253. platform_err:
  254. if (cpu_dai->ops->shutdown)
  255. cpu_dai->ops->shutdown(substream, cpu_dai);
  256. out:
  257. mutex_unlock(&pcm_mutex);
  258. return ret;
  259. }
  260. /*
  261. * Power down the audio subsystem pmdown_time msecs after close is called.
  262. * This is to ensure there are no pops or clicks in between any music tracks
  263. * due to DAPM power cycling.
  264. */
  265. static void close_delayed_work(struct work_struct *work)
  266. {
  267. struct snd_soc_card *card = container_of(work, struct snd_soc_card,
  268. delayed_work.work);
  269. struct snd_soc_device *socdev = card->socdev;
  270. struct snd_soc_codec *codec = card->codec;
  271. struct snd_soc_dai *codec_dai;
  272. int i;
  273. mutex_lock(&pcm_mutex);
  274. for (i = 0; i < codec->num_dai; i++) {
  275. codec_dai = &codec->dai[i];
  276. pr_debug("pop wq checking: %s status: %s waiting: %s\n",
  277. codec_dai->playback.stream_name,
  278. codec_dai->playback.active ? "active" : "inactive",
  279. codec_dai->pop_wait ? "yes" : "no");
  280. /* are we waiting on this codec DAI stream */
  281. if (codec_dai->pop_wait == 1) {
  282. /* Reduce power if no longer active */
  283. if (codec->active == 0) {
  284. pr_debug("pop wq D1 %s %s\n", codec->name,
  285. codec_dai->playback.stream_name);
  286. snd_soc_dapm_set_bias_level(socdev,
  287. SND_SOC_BIAS_PREPARE);
  288. }
  289. codec_dai->pop_wait = 0;
  290. snd_soc_dapm_stream_event(codec,
  291. codec_dai->playback.stream_name,
  292. SND_SOC_DAPM_STREAM_STOP);
  293. /* Fall into standby if no longer active */
  294. if (codec->active == 0) {
  295. pr_debug("pop wq D3 %s %s\n", codec->name,
  296. codec_dai->playback.stream_name);
  297. snd_soc_dapm_set_bias_level(socdev,
  298. SND_SOC_BIAS_STANDBY);
  299. }
  300. }
  301. }
  302. mutex_unlock(&pcm_mutex);
  303. }
  304. /*
  305. * Called by ALSA when a PCM substream is closed. Private data can be
  306. * freed here. The cpu DAI, codec DAI, machine and platform are also
  307. * shutdown.
  308. */
  309. static int soc_codec_close(struct snd_pcm_substream *substream)
  310. {
  311. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  312. struct snd_soc_device *socdev = rtd->socdev;
  313. struct snd_soc_card *card = socdev->card;
  314. struct snd_soc_dai_link *machine = rtd->dai;
  315. struct snd_soc_platform *platform = card->platform;
  316. struct snd_soc_dai *cpu_dai = machine->cpu_dai;
  317. struct snd_soc_dai *codec_dai = machine->codec_dai;
  318. struct snd_soc_codec *codec = card->codec;
  319. mutex_lock(&pcm_mutex);
  320. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  321. cpu_dai->playback.active = codec_dai->playback.active = 0;
  322. else
  323. cpu_dai->capture.active = codec_dai->capture.active = 0;
  324. if (codec_dai->playback.active == 0 &&
  325. codec_dai->capture.active == 0) {
  326. cpu_dai->active = codec_dai->active = 0;
  327. }
  328. codec->active--;
  329. /* Muting the DAC suppresses artifacts caused during digital
  330. * shutdown, for example from stopping clocks.
  331. */
  332. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  333. snd_soc_dai_digital_mute(codec_dai, 1);
  334. if (cpu_dai->ops->shutdown)
  335. cpu_dai->ops->shutdown(substream, cpu_dai);
  336. if (codec_dai->ops->shutdown)
  337. codec_dai->ops->shutdown(substream, codec_dai);
  338. if (machine->ops && machine->ops->shutdown)
  339. machine->ops->shutdown(substream);
  340. if (platform->pcm_ops->close)
  341. platform->pcm_ops->close(substream);
  342. cpu_dai->runtime = NULL;
  343. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  344. /* start delayed pop wq here for playback streams */
  345. codec_dai->pop_wait = 1;
  346. schedule_delayed_work(&card->delayed_work,
  347. msecs_to_jiffies(pmdown_time));
  348. } else {
  349. /* capture streams can be powered down now */
  350. snd_soc_dapm_stream_event(codec,
  351. codec_dai->capture.stream_name,
  352. SND_SOC_DAPM_STREAM_STOP);
  353. if (codec->active == 0 && codec_dai->pop_wait == 0)
  354. snd_soc_dapm_set_bias_level(socdev,
  355. SND_SOC_BIAS_STANDBY);
  356. }
  357. mutex_unlock(&pcm_mutex);
  358. return 0;
  359. }
  360. /*
  361. * Called by ALSA when the PCM substream is prepared, can set format, sample
  362. * rate, etc. This function is non atomic and can be called multiple times,
  363. * it can refer to the runtime info.
  364. */
  365. static int soc_pcm_prepare(struct snd_pcm_substream *substream)
  366. {
  367. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  368. struct snd_soc_device *socdev = rtd->socdev;
  369. struct snd_soc_card *card = socdev->card;
  370. struct snd_soc_dai_link *machine = rtd->dai;
  371. struct snd_soc_platform *platform = card->platform;
  372. struct snd_soc_dai *cpu_dai = machine->cpu_dai;
  373. struct snd_soc_dai *codec_dai = machine->codec_dai;
  374. struct snd_soc_codec *codec = card->codec;
  375. int ret = 0;
  376. mutex_lock(&pcm_mutex);
  377. if (machine->ops && machine->ops->prepare) {
  378. ret = machine->ops->prepare(substream);
  379. if (ret < 0) {
  380. printk(KERN_ERR "asoc: machine prepare error\n");
  381. goto out;
  382. }
  383. }
  384. if (platform->pcm_ops->prepare) {
  385. ret = platform->pcm_ops->prepare(substream);
  386. if (ret < 0) {
  387. printk(KERN_ERR "asoc: platform prepare error\n");
  388. goto out;
  389. }
  390. }
  391. if (codec_dai->ops->prepare) {
  392. ret = codec_dai->ops->prepare(substream, codec_dai);
  393. if (ret < 0) {
  394. printk(KERN_ERR "asoc: codec DAI prepare error\n");
  395. goto out;
  396. }
  397. }
  398. if (cpu_dai->ops->prepare) {
  399. ret = cpu_dai->ops->prepare(substream, cpu_dai);
  400. if (ret < 0) {
  401. printk(KERN_ERR "asoc: cpu DAI prepare error\n");
  402. goto out;
  403. }
  404. }
  405. /* cancel any delayed stream shutdown that is pending */
  406. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
  407. codec_dai->pop_wait) {
  408. codec_dai->pop_wait = 0;
  409. cancel_delayed_work(&card->delayed_work);
  410. }
  411. /* do we need to power up codec */
  412. if (codec->bias_level != SND_SOC_BIAS_ON) {
  413. snd_soc_dapm_set_bias_level(socdev,
  414. SND_SOC_BIAS_PREPARE);
  415. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  416. snd_soc_dapm_stream_event(codec,
  417. codec_dai->playback.stream_name,
  418. SND_SOC_DAPM_STREAM_START);
  419. else
  420. snd_soc_dapm_stream_event(codec,
  421. codec_dai->capture.stream_name,
  422. SND_SOC_DAPM_STREAM_START);
  423. snd_soc_dapm_set_bias_level(socdev, SND_SOC_BIAS_ON);
  424. snd_soc_dai_digital_mute(codec_dai, 0);
  425. } else {
  426. /* codec already powered - power on widgets */
  427. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  428. snd_soc_dapm_stream_event(codec,
  429. codec_dai->playback.stream_name,
  430. SND_SOC_DAPM_STREAM_START);
  431. else
  432. snd_soc_dapm_stream_event(codec,
  433. codec_dai->capture.stream_name,
  434. SND_SOC_DAPM_STREAM_START);
  435. snd_soc_dai_digital_mute(codec_dai, 0);
  436. }
  437. out:
  438. mutex_unlock(&pcm_mutex);
  439. return ret;
  440. }
  441. /*
  442. * Called by ALSA when the hardware params are set by application. This
  443. * function can also be called multiple times and can allocate buffers
  444. * (using snd_pcm_lib_* ). It's non-atomic.
  445. */
  446. static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
  447. struct snd_pcm_hw_params *params)
  448. {
  449. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  450. struct snd_soc_device *socdev = rtd->socdev;
  451. struct snd_soc_dai_link *machine = rtd->dai;
  452. struct snd_soc_card *card = socdev->card;
  453. struct snd_soc_platform *platform = card->platform;
  454. struct snd_soc_dai *cpu_dai = machine->cpu_dai;
  455. struct snd_soc_dai *codec_dai = machine->codec_dai;
  456. int ret = 0;
  457. mutex_lock(&pcm_mutex);
  458. if (machine->ops && machine->ops->hw_params) {
  459. ret = machine->ops->hw_params(substream, params);
  460. if (ret < 0) {
  461. printk(KERN_ERR "asoc: machine hw_params failed\n");
  462. goto out;
  463. }
  464. }
  465. if (codec_dai->ops->hw_params) {
  466. ret = codec_dai->ops->hw_params(substream, params, codec_dai);
  467. if (ret < 0) {
  468. printk(KERN_ERR "asoc: can't set codec %s hw params\n",
  469. codec_dai->name);
  470. goto codec_err;
  471. }
  472. }
  473. if (cpu_dai->ops->hw_params) {
  474. ret = cpu_dai->ops->hw_params(substream, params, cpu_dai);
  475. if (ret < 0) {
  476. printk(KERN_ERR "asoc: interface %s hw params failed\n",
  477. cpu_dai->name);
  478. goto interface_err;
  479. }
  480. }
  481. if (platform->pcm_ops->hw_params) {
  482. ret = platform->pcm_ops->hw_params(substream, params);
  483. if (ret < 0) {
  484. printk(KERN_ERR "asoc: platform %s hw params failed\n",
  485. platform->name);
  486. goto platform_err;
  487. }
  488. }
  489. machine->rate = params_rate(params);
  490. out:
  491. mutex_unlock(&pcm_mutex);
  492. return ret;
  493. platform_err:
  494. if (cpu_dai->ops->hw_free)
  495. cpu_dai->ops->hw_free(substream, cpu_dai);
  496. interface_err:
  497. if (codec_dai->ops->hw_free)
  498. codec_dai->ops->hw_free(substream, codec_dai);
  499. codec_err:
  500. if (machine->ops && machine->ops->hw_free)
  501. machine->ops->hw_free(substream);
  502. mutex_unlock(&pcm_mutex);
  503. return ret;
  504. }
  505. /*
  506. * Free's resources allocated by hw_params, can be called multiple times
  507. */
  508. static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
  509. {
  510. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  511. struct snd_soc_device *socdev = rtd->socdev;
  512. struct snd_soc_dai_link *machine = rtd->dai;
  513. struct snd_soc_card *card = socdev->card;
  514. struct snd_soc_platform *platform = card->platform;
  515. struct snd_soc_dai *cpu_dai = machine->cpu_dai;
  516. struct snd_soc_dai *codec_dai = machine->codec_dai;
  517. struct snd_soc_codec *codec = card->codec;
  518. mutex_lock(&pcm_mutex);
  519. /* apply codec digital mute */
  520. if (!codec->active)
  521. snd_soc_dai_digital_mute(codec_dai, 1);
  522. /* free any machine hw params */
  523. if (machine->ops && machine->ops->hw_free)
  524. machine->ops->hw_free(substream);
  525. /* free any DMA resources */
  526. if (platform->pcm_ops->hw_free)
  527. platform->pcm_ops->hw_free(substream);
  528. /* now free hw params for the DAI's */
  529. if (codec_dai->ops->hw_free)
  530. codec_dai->ops->hw_free(substream, codec_dai);
  531. if (cpu_dai->ops->hw_free)
  532. cpu_dai->ops->hw_free(substream, cpu_dai);
  533. mutex_unlock(&pcm_mutex);
  534. return 0;
  535. }
  536. static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  537. {
  538. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  539. struct snd_soc_device *socdev = rtd->socdev;
  540. struct snd_soc_card *card= socdev->card;
  541. struct snd_soc_dai_link *machine = rtd->dai;
  542. struct snd_soc_platform *platform = card->platform;
  543. struct snd_soc_dai *cpu_dai = machine->cpu_dai;
  544. struct snd_soc_dai *codec_dai = machine->codec_dai;
  545. int ret;
  546. if (codec_dai->ops->trigger) {
  547. ret = codec_dai->ops->trigger(substream, cmd, codec_dai);
  548. if (ret < 0)
  549. return ret;
  550. }
  551. if (platform->pcm_ops->trigger) {
  552. ret = platform->pcm_ops->trigger(substream, cmd);
  553. if (ret < 0)
  554. return ret;
  555. }
  556. if (cpu_dai->ops->trigger) {
  557. ret = cpu_dai->ops->trigger(substream, cmd, cpu_dai);
  558. if (ret < 0)
  559. return ret;
  560. }
  561. return 0;
  562. }
  563. /* ASoC PCM operations */
  564. static struct snd_pcm_ops soc_pcm_ops = {
  565. .open = soc_pcm_open,
  566. .close = soc_codec_close,
  567. .hw_params = soc_pcm_hw_params,
  568. .hw_free = soc_pcm_hw_free,
  569. .prepare = soc_pcm_prepare,
  570. .trigger = soc_pcm_trigger,
  571. };
  572. #ifdef CONFIG_PM
  573. /* powers down audio subsystem for suspend */
  574. static int soc_suspend(struct platform_device *pdev, pm_message_t state)
  575. {
  576. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  577. struct snd_soc_card *card = socdev->card;
  578. struct snd_soc_platform *platform = card->platform;
  579. struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
  580. struct snd_soc_codec *codec = card->codec;
  581. int i;
  582. /* Due to the resume being scheduled into a workqueue we could
  583. * suspend before that's finished - wait for it to complete.
  584. */
  585. snd_power_lock(codec->card);
  586. snd_power_wait(codec->card, SNDRV_CTL_POWER_D0);
  587. snd_power_unlock(codec->card);
  588. /* we're going to block userspace touching us until resume completes */
  589. snd_power_change_state(codec->card, SNDRV_CTL_POWER_D3hot);
  590. /* mute any active DAC's */
  591. for (i = 0; i < card->num_links; i++) {
  592. struct snd_soc_dai *dai = card->dai_link[i].codec_dai;
  593. if (dai->ops->digital_mute && dai->playback.active)
  594. dai->ops->digital_mute(dai, 1);
  595. }
  596. /* suspend all pcms */
  597. for (i = 0; i < card->num_links; i++)
  598. snd_pcm_suspend_all(card->dai_link[i].pcm);
  599. if (card->suspend_pre)
  600. card->suspend_pre(pdev, state);
  601. for (i = 0; i < card->num_links; i++) {
  602. struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
  603. if (cpu_dai->suspend && !cpu_dai->ac97_control)
  604. cpu_dai->suspend(cpu_dai);
  605. if (platform->suspend)
  606. platform->suspend(cpu_dai);
  607. }
  608. /* close any waiting streams and save state */
  609. run_delayed_work(&card->delayed_work);
  610. codec->suspend_bias_level = codec->bias_level;
  611. for (i = 0; i < codec->num_dai; i++) {
  612. char *stream = codec->dai[i].playback.stream_name;
  613. if (stream != NULL)
  614. snd_soc_dapm_stream_event(codec, stream,
  615. SND_SOC_DAPM_STREAM_SUSPEND);
  616. stream = codec->dai[i].capture.stream_name;
  617. if (stream != NULL)
  618. snd_soc_dapm_stream_event(codec, stream,
  619. SND_SOC_DAPM_STREAM_SUSPEND);
  620. }
  621. if (codec_dev->suspend)
  622. codec_dev->suspend(pdev, state);
  623. for (i = 0; i < card->num_links; i++) {
  624. struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
  625. if (cpu_dai->suspend && cpu_dai->ac97_control)
  626. cpu_dai->suspend(cpu_dai);
  627. }
  628. if (card->suspend_post)
  629. card->suspend_post(pdev, state);
  630. return 0;
  631. }
  632. /* deferred resume work, so resume can complete before we finished
  633. * setting our codec back up, which can be very slow on I2C
  634. */
  635. static void soc_resume_deferred(struct work_struct *work)
  636. {
  637. struct snd_soc_card *card = container_of(work,
  638. struct snd_soc_card,
  639. deferred_resume_work);
  640. struct snd_soc_device *socdev = card->socdev;
  641. struct snd_soc_platform *platform = card->platform;
  642. struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
  643. struct snd_soc_codec *codec = card->codec;
  644. struct platform_device *pdev = to_platform_device(socdev->dev);
  645. int i;
  646. /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
  647. * so userspace apps are blocked from touching us
  648. */
  649. dev_dbg(socdev->dev, "starting resume work\n");
  650. if (card->resume_pre)
  651. card->resume_pre(pdev);
  652. for (i = 0; i < card->num_links; i++) {
  653. struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
  654. if (cpu_dai->resume && cpu_dai->ac97_control)
  655. cpu_dai->resume(cpu_dai);
  656. }
  657. if (codec_dev->resume)
  658. codec_dev->resume(pdev);
  659. for (i = 0; i < codec->num_dai; i++) {
  660. char *stream = codec->dai[i].playback.stream_name;
  661. if (stream != NULL)
  662. snd_soc_dapm_stream_event(codec, stream,
  663. SND_SOC_DAPM_STREAM_RESUME);
  664. stream = codec->dai[i].capture.stream_name;
  665. if (stream != NULL)
  666. snd_soc_dapm_stream_event(codec, stream,
  667. SND_SOC_DAPM_STREAM_RESUME);
  668. }
  669. /* unmute any active DACs */
  670. for (i = 0; i < card->num_links; i++) {
  671. struct snd_soc_dai *dai = card->dai_link[i].codec_dai;
  672. if (dai->ops->digital_mute && dai->playback.active)
  673. dai->ops->digital_mute(dai, 0);
  674. }
  675. for (i = 0; i < card->num_links; i++) {
  676. struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
  677. if (cpu_dai->resume && !cpu_dai->ac97_control)
  678. cpu_dai->resume(cpu_dai);
  679. if (platform->resume)
  680. platform->resume(cpu_dai);
  681. }
  682. if (card->resume_post)
  683. card->resume_post(pdev);
  684. dev_dbg(socdev->dev, "resume work completed\n");
  685. /* userspace can access us now we are back as we were before */
  686. snd_power_change_state(codec->card, SNDRV_CTL_POWER_D0);
  687. }
  688. /* powers up audio subsystem after a suspend */
  689. static int soc_resume(struct platform_device *pdev)
  690. {
  691. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  692. struct snd_soc_card *card = socdev->card;
  693. struct snd_soc_dai *cpu_dai = card->dai_link[0].cpu_dai;
  694. /* AC97 devices might have other drivers hanging off them so
  695. * need to resume immediately. Other drivers don't have that
  696. * problem and may take a substantial amount of time to resume
  697. * due to I/O costs and anti-pop so handle them out of line.
  698. */
  699. if (cpu_dai->ac97_control) {
  700. dev_dbg(socdev->dev, "Resuming AC97 immediately\n");
  701. soc_resume_deferred(&card->deferred_resume_work);
  702. } else {
  703. dev_dbg(socdev->dev, "Scheduling resume work\n");
  704. if (!schedule_work(&card->deferred_resume_work))
  705. dev_err(socdev->dev, "resume work item may be lost\n");
  706. }
  707. return 0;
  708. }
  709. #else
  710. #define soc_suspend NULL
  711. #define soc_resume NULL
  712. #endif
  713. static void snd_soc_instantiate_card(struct snd_soc_card *card)
  714. {
  715. struct platform_device *pdev = container_of(card->dev,
  716. struct platform_device,
  717. dev);
  718. struct snd_soc_codec_device *codec_dev = card->socdev->codec_dev;
  719. struct snd_soc_platform *platform;
  720. struct snd_soc_dai *dai;
  721. int i, found, ret, ac97;
  722. if (card->instantiated)
  723. return;
  724. found = 0;
  725. list_for_each_entry(platform, &platform_list, list)
  726. if (card->platform == platform) {
  727. found = 1;
  728. break;
  729. }
  730. if (!found) {
  731. dev_dbg(card->dev, "Platform %s not registered\n",
  732. card->platform->name);
  733. return;
  734. }
  735. ac97 = 0;
  736. for (i = 0; i < card->num_links; i++) {
  737. found = 0;
  738. list_for_each_entry(dai, &dai_list, list)
  739. if (card->dai_link[i].cpu_dai == dai) {
  740. found = 1;
  741. break;
  742. }
  743. if (!found) {
  744. dev_dbg(card->dev, "DAI %s not registered\n",
  745. card->dai_link[i].cpu_dai->name);
  746. return;
  747. }
  748. if (card->dai_link[i].cpu_dai->ac97_control)
  749. ac97 = 1;
  750. }
  751. /* If we have AC97 in the system then don't wait for the
  752. * codec. This will need revisiting if we have to handle
  753. * systems with mixed AC97 and non-AC97 parts. Only check for
  754. * DAIs currently; we can't do this per link since some AC97
  755. * codecs have non-AC97 DAIs.
  756. */
  757. if (!ac97)
  758. for (i = 0; i < card->num_links; i++) {
  759. found = 0;
  760. list_for_each_entry(dai, &dai_list, list)
  761. if (card->dai_link[i].codec_dai == dai) {
  762. found = 1;
  763. break;
  764. }
  765. if (!found) {
  766. dev_dbg(card->dev, "DAI %s not registered\n",
  767. card->dai_link[i].codec_dai->name);
  768. return;
  769. }
  770. }
  771. /* Note that we do not current check for codec components */
  772. dev_dbg(card->dev, "All components present, instantiating\n");
  773. /* Found everything, bring it up */
  774. if (card->probe) {
  775. ret = card->probe(pdev);
  776. if (ret < 0)
  777. return;
  778. }
  779. for (i = 0; i < card->num_links; i++) {
  780. struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
  781. if (cpu_dai->probe) {
  782. ret = cpu_dai->probe(pdev, cpu_dai);
  783. if (ret < 0)
  784. goto cpu_dai_err;
  785. }
  786. }
  787. if (codec_dev->probe) {
  788. ret = codec_dev->probe(pdev);
  789. if (ret < 0)
  790. goto cpu_dai_err;
  791. }
  792. if (platform->probe) {
  793. ret = platform->probe(pdev);
  794. if (ret < 0)
  795. goto platform_err;
  796. }
  797. /* DAPM stream work */
  798. INIT_DELAYED_WORK(&card->delayed_work, close_delayed_work);
  799. #ifdef CONFIG_PM
  800. /* deferred resume work */
  801. INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
  802. #endif
  803. card->instantiated = 1;
  804. return;
  805. platform_err:
  806. if (codec_dev->remove)
  807. codec_dev->remove(pdev);
  808. cpu_dai_err:
  809. for (i--; i >= 0; i--) {
  810. struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
  811. if (cpu_dai->remove)
  812. cpu_dai->remove(pdev, cpu_dai);
  813. }
  814. if (card->remove)
  815. card->remove(pdev);
  816. }
  817. /*
  818. * Attempt to initialise any uninitalised cards. Must be called with
  819. * client_mutex.
  820. */
  821. static void snd_soc_instantiate_cards(void)
  822. {
  823. struct snd_soc_card *card;
  824. list_for_each_entry(card, &card_list, list)
  825. snd_soc_instantiate_card(card);
  826. }
  827. /* probes a new socdev */
  828. static int soc_probe(struct platform_device *pdev)
  829. {
  830. int ret = 0;
  831. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  832. struct snd_soc_card *card = socdev->card;
  833. /* Bodge while we push things out of socdev */
  834. card->socdev = socdev;
  835. /* Bodge while we unpick instantiation */
  836. card->dev = &pdev->dev;
  837. ret = snd_soc_register_card(card);
  838. if (ret != 0) {
  839. dev_err(&pdev->dev, "Failed to register card\n");
  840. return ret;
  841. }
  842. return 0;
  843. }
  844. /* removes a socdev */
  845. static int soc_remove(struct platform_device *pdev)
  846. {
  847. int i;
  848. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  849. struct snd_soc_card *card = socdev->card;
  850. struct snd_soc_platform *platform = card->platform;
  851. struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
  852. run_delayed_work(&card->delayed_work);
  853. if (platform->remove)
  854. platform->remove(pdev);
  855. if (codec_dev->remove)
  856. codec_dev->remove(pdev);
  857. for (i = 0; i < card->num_links; i++) {
  858. struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
  859. if (cpu_dai->remove)
  860. cpu_dai->remove(pdev, cpu_dai);
  861. }
  862. if (card->remove)
  863. card->remove(pdev);
  864. snd_soc_unregister_card(card);
  865. return 0;
  866. }
  867. /* ASoC platform driver */
  868. static struct platform_driver soc_driver = {
  869. .driver = {
  870. .name = "soc-audio",
  871. .owner = THIS_MODULE,
  872. },
  873. .probe = soc_probe,
  874. .remove = soc_remove,
  875. .suspend = soc_suspend,
  876. .resume = soc_resume,
  877. };
  878. /* create a new pcm */
  879. static int soc_new_pcm(struct snd_soc_device *socdev,
  880. struct snd_soc_dai_link *dai_link, int num)
  881. {
  882. struct snd_soc_card *card = socdev->card;
  883. struct snd_soc_codec *codec = card->codec;
  884. struct snd_soc_platform *platform = card->platform;
  885. struct snd_soc_dai *codec_dai = dai_link->codec_dai;
  886. struct snd_soc_dai *cpu_dai = dai_link->cpu_dai;
  887. struct snd_soc_pcm_runtime *rtd;
  888. struct snd_pcm *pcm;
  889. char new_name[64];
  890. int ret = 0, playback = 0, capture = 0;
  891. rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
  892. if (rtd == NULL)
  893. return -ENOMEM;
  894. rtd->dai = dai_link;
  895. rtd->socdev = socdev;
  896. codec_dai->codec = card->codec;
  897. /* check client and interface hw capabilities */
  898. sprintf(new_name, "%s %s-%d", dai_link->stream_name, codec_dai->name,
  899. num);
  900. if (codec_dai->playback.channels_min)
  901. playback = 1;
  902. if (codec_dai->capture.channels_min)
  903. capture = 1;
  904. ret = snd_pcm_new(codec->card, new_name, codec->pcm_devs++, playback,
  905. capture, &pcm);
  906. if (ret < 0) {
  907. printk(KERN_ERR "asoc: can't create pcm for codec %s\n",
  908. codec->name);
  909. kfree(rtd);
  910. return ret;
  911. }
  912. dai_link->pcm = pcm;
  913. pcm->private_data = rtd;
  914. soc_pcm_ops.mmap = platform->pcm_ops->mmap;
  915. soc_pcm_ops.pointer = platform->pcm_ops->pointer;
  916. soc_pcm_ops.ioctl = platform->pcm_ops->ioctl;
  917. soc_pcm_ops.copy = platform->pcm_ops->copy;
  918. soc_pcm_ops.silence = platform->pcm_ops->silence;
  919. soc_pcm_ops.ack = platform->pcm_ops->ack;
  920. soc_pcm_ops.page = platform->pcm_ops->page;
  921. if (playback)
  922. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
  923. if (capture)
  924. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
  925. ret = platform->pcm_new(codec->card, codec_dai, pcm);
  926. if (ret < 0) {
  927. printk(KERN_ERR "asoc: platform pcm constructor failed\n");
  928. kfree(rtd);
  929. return ret;
  930. }
  931. pcm->private_free = platform->pcm_free;
  932. printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
  933. cpu_dai->name);
  934. return ret;
  935. }
  936. /* codec register dump */
  937. static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf)
  938. {
  939. int i, step = 1, count = 0;
  940. if (!codec->reg_cache_size)
  941. return 0;
  942. if (codec->reg_cache_step)
  943. step = codec->reg_cache_step;
  944. count += sprintf(buf, "%s registers\n", codec->name);
  945. for (i = 0; i < codec->reg_cache_size; i += step) {
  946. count += sprintf(buf + count, "%2x: ", i);
  947. if (count >= PAGE_SIZE - 1)
  948. break;
  949. if (codec->display_register)
  950. count += codec->display_register(codec, buf + count,
  951. PAGE_SIZE - count, i);
  952. else
  953. count += snprintf(buf + count, PAGE_SIZE - count,
  954. "%4x", codec->read(codec, i));
  955. if (count >= PAGE_SIZE - 1)
  956. break;
  957. count += snprintf(buf + count, PAGE_SIZE - count, "\n");
  958. if (count >= PAGE_SIZE - 1)
  959. break;
  960. }
  961. /* Truncate count; min() would cause a warning */
  962. if (count >= PAGE_SIZE)
  963. count = PAGE_SIZE - 1;
  964. return count;
  965. }
  966. static ssize_t codec_reg_show(struct device *dev,
  967. struct device_attribute *attr, char *buf)
  968. {
  969. struct snd_soc_device *devdata = dev_get_drvdata(dev);
  970. return soc_codec_reg_show(devdata->card->codec, buf);
  971. }
  972. static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
  973. #ifdef CONFIG_DEBUG_FS
  974. static int codec_reg_open_file(struct inode *inode, struct file *file)
  975. {
  976. file->private_data = inode->i_private;
  977. return 0;
  978. }
  979. static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
  980. size_t count, loff_t *ppos)
  981. {
  982. ssize_t ret;
  983. struct snd_soc_codec *codec = file->private_data;
  984. char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  985. if (!buf)
  986. return -ENOMEM;
  987. ret = soc_codec_reg_show(codec, buf);
  988. if (ret >= 0)
  989. ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
  990. kfree(buf);
  991. return ret;
  992. }
  993. static ssize_t codec_reg_write_file(struct file *file,
  994. const char __user *user_buf, size_t count, loff_t *ppos)
  995. {
  996. char buf[32];
  997. int buf_size;
  998. char *start = buf;
  999. unsigned long reg, value;
  1000. int step = 1;
  1001. struct snd_soc_codec *codec = file->private_data;
  1002. buf_size = min(count, (sizeof(buf)-1));
  1003. if (copy_from_user(buf, user_buf, buf_size))
  1004. return -EFAULT;
  1005. buf[buf_size] = 0;
  1006. if (codec->reg_cache_step)
  1007. step = codec->reg_cache_step;
  1008. while (*start == ' ')
  1009. start++;
  1010. reg = simple_strtoul(start, &start, 16);
  1011. if ((reg >= codec->reg_cache_size) || (reg % step))
  1012. return -EINVAL;
  1013. while (*start == ' ')
  1014. start++;
  1015. if (strict_strtoul(start, 16, &value))
  1016. return -EINVAL;
  1017. codec->write(codec, reg, value);
  1018. return buf_size;
  1019. }
  1020. static const struct file_operations codec_reg_fops = {
  1021. .open = codec_reg_open_file,
  1022. .read = codec_reg_read_file,
  1023. .write = codec_reg_write_file,
  1024. };
  1025. static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
  1026. {
  1027. codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
  1028. debugfs_root, codec,
  1029. &codec_reg_fops);
  1030. if (!codec->debugfs_reg)
  1031. printk(KERN_WARNING
  1032. "ASoC: Failed to create codec register debugfs file\n");
  1033. codec->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0744,
  1034. debugfs_root,
  1035. &codec->pop_time);
  1036. if (!codec->debugfs_pop_time)
  1037. printk(KERN_WARNING
  1038. "Failed to create pop time debugfs file\n");
  1039. }
  1040. static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
  1041. {
  1042. debugfs_remove(codec->debugfs_pop_time);
  1043. debugfs_remove(codec->debugfs_reg);
  1044. }
  1045. #else
  1046. static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
  1047. {
  1048. }
  1049. static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
  1050. {
  1051. }
  1052. #endif
  1053. /**
  1054. * snd_soc_new_ac97_codec - initailise AC97 device
  1055. * @codec: audio codec
  1056. * @ops: AC97 bus operations
  1057. * @num: AC97 codec number
  1058. *
  1059. * Initialises AC97 codec resources for use by ad-hoc devices only.
  1060. */
  1061. int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
  1062. struct snd_ac97_bus_ops *ops, int num)
  1063. {
  1064. mutex_lock(&codec->mutex);
  1065. codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
  1066. if (codec->ac97 == NULL) {
  1067. mutex_unlock(&codec->mutex);
  1068. return -ENOMEM;
  1069. }
  1070. codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
  1071. if (codec->ac97->bus == NULL) {
  1072. kfree(codec->ac97);
  1073. codec->ac97 = NULL;
  1074. mutex_unlock(&codec->mutex);
  1075. return -ENOMEM;
  1076. }
  1077. codec->ac97->bus->ops = ops;
  1078. codec->ac97->num = num;
  1079. mutex_unlock(&codec->mutex);
  1080. return 0;
  1081. }
  1082. EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
  1083. /**
  1084. * snd_soc_free_ac97_codec - free AC97 codec device
  1085. * @codec: audio codec
  1086. *
  1087. * Frees AC97 codec device resources.
  1088. */
  1089. void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
  1090. {
  1091. mutex_lock(&codec->mutex);
  1092. kfree(codec->ac97->bus);
  1093. kfree(codec->ac97);
  1094. codec->ac97 = NULL;
  1095. mutex_unlock(&codec->mutex);
  1096. }
  1097. EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
  1098. /**
  1099. * snd_soc_update_bits - update codec register bits
  1100. * @codec: audio codec
  1101. * @reg: codec register
  1102. * @mask: register mask
  1103. * @value: new value
  1104. *
  1105. * Writes new register value.
  1106. *
  1107. * Returns 1 for change else 0.
  1108. */
  1109. int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
  1110. unsigned short mask, unsigned short value)
  1111. {
  1112. int change;
  1113. unsigned short old, new;
  1114. mutex_lock(&io_mutex);
  1115. old = snd_soc_read(codec, reg);
  1116. new = (old & ~mask) | value;
  1117. change = old != new;
  1118. if (change)
  1119. snd_soc_write(codec, reg, new);
  1120. mutex_unlock(&io_mutex);
  1121. return change;
  1122. }
  1123. EXPORT_SYMBOL_GPL(snd_soc_update_bits);
  1124. /**
  1125. * snd_soc_test_bits - test register for change
  1126. * @codec: audio codec
  1127. * @reg: codec register
  1128. * @mask: register mask
  1129. * @value: new value
  1130. *
  1131. * Tests a register with a new value and checks if the new value is
  1132. * different from the old value.
  1133. *
  1134. * Returns 1 for change else 0.
  1135. */
  1136. int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
  1137. unsigned short mask, unsigned short value)
  1138. {
  1139. int change;
  1140. unsigned short old, new;
  1141. mutex_lock(&io_mutex);
  1142. old = snd_soc_read(codec, reg);
  1143. new = (old & ~mask) | value;
  1144. change = old != new;
  1145. mutex_unlock(&io_mutex);
  1146. return change;
  1147. }
  1148. EXPORT_SYMBOL_GPL(snd_soc_test_bits);
  1149. /**
  1150. * snd_soc_new_pcms - create new sound card and pcms
  1151. * @socdev: the SoC audio device
  1152. * @idx: ALSA card index
  1153. * @xid: card identification
  1154. *
  1155. * Create a new sound card based upon the codec and interface pcms.
  1156. *
  1157. * Returns 0 for success, else error.
  1158. */
  1159. int snd_soc_new_pcms(struct snd_soc_device *socdev, int idx, const char *xid)
  1160. {
  1161. struct snd_soc_card *card = socdev->card;
  1162. struct snd_soc_codec *codec = card->codec;
  1163. int ret, i;
  1164. mutex_lock(&codec->mutex);
  1165. /* register a sound card */
  1166. ret = snd_card_create(idx, xid, codec->owner, 0, &codec->card);
  1167. if (ret < 0) {
  1168. printk(KERN_ERR "asoc: can't create sound card for codec %s\n",
  1169. codec->name);
  1170. mutex_unlock(&codec->mutex);
  1171. return ret;
  1172. }
  1173. codec->card->dev = socdev->dev;
  1174. codec->card->private_data = codec;
  1175. strncpy(codec->card->driver, codec->name, sizeof(codec->card->driver));
  1176. /* create the pcms */
  1177. for (i = 0; i < card->num_links; i++) {
  1178. ret = soc_new_pcm(socdev, &card->dai_link[i], i);
  1179. if (ret < 0) {
  1180. printk(KERN_ERR "asoc: can't create pcm %s\n",
  1181. card->dai_link[i].stream_name);
  1182. mutex_unlock(&codec->mutex);
  1183. return ret;
  1184. }
  1185. }
  1186. mutex_unlock(&codec->mutex);
  1187. return ret;
  1188. }
  1189. EXPORT_SYMBOL_GPL(snd_soc_new_pcms);
  1190. /**
  1191. * snd_soc_init_card - register sound card
  1192. * @socdev: the SoC audio device
  1193. *
  1194. * Register a SoC sound card. Also registers an AC97 device if the
  1195. * codec is AC97 for ad hoc devices.
  1196. *
  1197. * Returns 0 for success, else error.
  1198. */
  1199. int snd_soc_init_card(struct snd_soc_device *socdev)
  1200. {
  1201. struct snd_soc_card *card = socdev->card;
  1202. struct snd_soc_codec *codec = card->codec;
  1203. int ret = 0, i, ac97 = 0, err = 0;
  1204. for (i = 0; i < card->num_links; i++) {
  1205. if (card->dai_link[i].init) {
  1206. err = card->dai_link[i].init(codec);
  1207. if (err < 0) {
  1208. printk(KERN_ERR "asoc: failed to init %s\n",
  1209. card->dai_link[i].stream_name);
  1210. continue;
  1211. }
  1212. }
  1213. if (card->dai_link[i].codec_dai->ac97_control)
  1214. ac97 = 1;
  1215. }
  1216. snprintf(codec->card->shortname, sizeof(codec->card->shortname),
  1217. "%s", card->name);
  1218. snprintf(codec->card->longname, sizeof(codec->card->longname),
  1219. "%s (%s)", card->name, codec->name);
  1220. ret = snd_card_register(codec->card);
  1221. if (ret < 0) {
  1222. printk(KERN_ERR "asoc: failed to register soundcard for %s\n",
  1223. codec->name);
  1224. goto out;
  1225. }
  1226. mutex_lock(&codec->mutex);
  1227. #ifdef CONFIG_SND_SOC_AC97_BUS
  1228. /* Only instantiate AC97 if not already done by the adaptor
  1229. * for the generic AC97 subsystem.
  1230. */
  1231. if (ac97 && strcmp(codec->name, "AC97") != 0) {
  1232. ret = soc_ac97_dev_register(codec);
  1233. if (ret < 0) {
  1234. printk(KERN_ERR "asoc: AC97 device register failed\n");
  1235. snd_card_free(codec->card);
  1236. mutex_unlock(&codec->mutex);
  1237. goto out;
  1238. }
  1239. }
  1240. #endif
  1241. err = snd_soc_dapm_sys_add(socdev->dev);
  1242. if (err < 0)
  1243. printk(KERN_WARNING "asoc: failed to add dapm sysfs entries\n");
  1244. err = device_create_file(socdev->dev, &dev_attr_codec_reg);
  1245. if (err < 0)
  1246. printk(KERN_WARNING "asoc: failed to add codec sysfs files\n");
  1247. soc_init_codec_debugfs(codec);
  1248. mutex_unlock(&codec->mutex);
  1249. out:
  1250. return ret;
  1251. }
  1252. EXPORT_SYMBOL_GPL(snd_soc_init_card);
  1253. /**
  1254. * snd_soc_free_pcms - free sound card and pcms
  1255. * @socdev: the SoC audio device
  1256. *
  1257. * Frees sound card and pcms associated with the socdev.
  1258. * Also unregister the codec if it is an AC97 device.
  1259. */
  1260. void snd_soc_free_pcms(struct snd_soc_device *socdev)
  1261. {
  1262. struct snd_soc_codec *codec = socdev->card->codec;
  1263. #ifdef CONFIG_SND_SOC_AC97_BUS
  1264. struct snd_soc_dai *codec_dai;
  1265. int i;
  1266. #endif
  1267. mutex_lock(&codec->mutex);
  1268. soc_cleanup_codec_debugfs(codec);
  1269. #ifdef CONFIG_SND_SOC_AC97_BUS
  1270. for (i = 0; i < codec->num_dai; i++) {
  1271. codec_dai = &codec->dai[i];
  1272. if (codec_dai->ac97_control && codec->ac97 &&
  1273. strcmp(codec->name, "AC97") != 0) {
  1274. soc_ac97_dev_unregister(codec);
  1275. goto free_card;
  1276. }
  1277. }
  1278. free_card:
  1279. #endif
  1280. if (codec->card)
  1281. snd_card_free(codec->card);
  1282. device_remove_file(socdev->dev, &dev_attr_codec_reg);
  1283. mutex_unlock(&codec->mutex);
  1284. }
  1285. EXPORT_SYMBOL_GPL(snd_soc_free_pcms);
  1286. /**
  1287. * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
  1288. * @substream: the pcm substream
  1289. * @hw: the hardware parameters
  1290. *
  1291. * Sets the substream runtime hardware parameters.
  1292. */
  1293. int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
  1294. const struct snd_pcm_hardware *hw)
  1295. {
  1296. struct snd_pcm_runtime *runtime = substream->runtime;
  1297. runtime->hw.info = hw->info;
  1298. runtime->hw.formats = hw->formats;
  1299. runtime->hw.period_bytes_min = hw->period_bytes_min;
  1300. runtime->hw.period_bytes_max = hw->period_bytes_max;
  1301. runtime->hw.periods_min = hw->periods_min;
  1302. runtime->hw.periods_max = hw->periods_max;
  1303. runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
  1304. runtime->hw.fifo_size = hw->fifo_size;
  1305. return 0;
  1306. }
  1307. EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
  1308. /**
  1309. * snd_soc_cnew - create new control
  1310. * @_template: control template
  1311. * @data: control private data
  1312. * @long_name: control long name
  1313. *
  1314. * Create a new mixer control from a template control.
  1315. *
  1316. * Returns 0 for success, else error.
  1317. */
  1318. struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
  1319. void *data, char *long_name)
  1320. {
  1321. struct snd_kcontrol_new template;
  1322. memcpy(&template, _template, sizeof(template));
  1323. if (long_name)
  1324. template.name = long_name;
  1325. template.index = 0;
  1326. return snd_ctl_new1(&template, data);
  1327. }
  1328. EXPORT_SYMBOL_GPL(snd_soc_cnew);
  1329. /**
  1330. * snd_soc_add_controls - add an array of controls to a codec.
  1331. * Convienience function to add a list of controls. Many codecs were
  1332. * duplicating this code.
  1333. *
  1334. * @codec: codec to add controls to
  1335. * @controls: array of controls to add
  1336. * @num_controls: number of elements in the array
  1337. *
  1338. * Return 0 for success, else error.
  1339. */
  1340. int snd_soc_add_controls(struct snd_soc_codec *codec,
  1341. const struct snd_kcontrol_new *controls, int num_controls)
  1342. {
  1343. struct snd_card *card = codec->card;
  1344. int err, i;
  1345. for (i = 0; i < num_controls; i++) {
  1346. const struct snd_kcontrol_new *control = &controls[i];
  1347. err = snd_ctl_add(card, snd_soc_cnew(control, codec, NULL));
  1348. if (err < 0) {
  1349. dev_err(codec->dev, "%s: Failed to add %s\n",
  1350. codec->name, control->name);
  1351. return err;
  1352. }
  1353. }
  1354. return 0;
  1355. }
  1356. EXPORT_SYMBOL_GPL(snd_soc_add_controls);
  1357. /**
  1358. * snd_soc_info_enum_double - enumerated double mixer info callback
  1359. * @kcontrol: mixer control
  1360. * @uinfo: control element information
  1361. *
  1362. * Callback to provide information about a double enumerated
  1363. * mixer control.
  1364. *
  1365. * Returns 0 for success.
  1366. */
  1367. int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
  1368. struct snd_ctl_elem_info *uinfo)
  1369. {
  1370. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1371. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  1372. uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
  1373. uinfo->value.enumerated.items = e->max;
  1374. if (uinfo->value.enumerated.item > e->max - 1)
  1375. uinfo->value.enumerated.item = e->max - 1;
  1376. strcpy(uinfo->value.enumerated.name,
  1377. e->texts[uinfo->value.enumerated.item]);
  1378. return 0;
  1379. }
  1380. EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
  1381. /**
  1382. * snd_soc_get_enum_double - enumerated double mixer get callback
  1383. * @kcontrol: mixer control
  1384. * @ucontrol: control element information
  1385. *
  1386. * Callback to get the value of a double enumerated mixer.
  1387. *
  1388. * Returns 0 for success.
  1389. */
  1390. int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
  1391. struct snd_ctl_elem_value *ucontrol)
  1392. {
  1393. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1394. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1395. unsigned short val, bitmask;
  1396. for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
  1397. ;
  1398. val = snd_soc_read(codec, e->reg);
  1399. ucontrol->value.enumerated.item[0]
  1400. = (val >> e->shift_l) & (bitmask - 1);
  1401. if (e->shift_l != e->shift_r)
  1402. ucontrol->value.enumerated.item[1] =
  1403. (val >> e->shift_r) & (bitmask - 1);
  1404. return 0;
  1405. }
  1406. EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
  1407. /**
  1408. * snd_soc_put_enum_double - enumerated double mixer put callback
  1409. * @kcontrol: mixer control
  1410. * @ucontrol: control element information
  1411. *
  1412. * Callback to set the value of a double enumerated mixer.
  1413. *
  1414. * Returns 0 for success.
  1415. */
  1416. int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
  1417. struct snd_ctl_elem_value *ucontrol)
  1418. {
  1419. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1420. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1421. unsigned short val;
  1422. unsigned short mask, bitmask;
  1423. for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
  1424. ;
  1425. if (ucontrol->value.enumerated.item[0] > e->max - 1)
  1426. return -EINVAL;
  1427. val = ucontrol->value.enumerated.item[0] << e->shift_l;
  1428. mask = (bitmask - 1) << e->shift_l;
  1429. if (e->shift_l != e->shift_r) {
  1430. if (ucontrol->value.enumerated.item[1] > e->max - 1)
  1431. return -EINVAL;
  1432. val |= ucontrol->value.enumerated.item[1] << e->shift_r;
  1433. mask |= (bitmask - 1) << e->shift_r;
  1434. }
  1435. return snd_soc_update_bits(codec, e->reg, mask, val);
  1436. }
  1437. EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
  1438. /**
  1439. * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
  1440. * @kcontrol: mixer control
  1441. * @ucontrol: control element information
  1442. *
  1443. * Callback to get the value of a double semi enumerated mixer.
  1444. *
  1445. * Semi enumerated mixer: the enumerated items are referred as values. Can be
  1446. * used for handling bitfield coded enumeration for example.
  1447. *
  1448. * Returns 0 for success.
  1449. */
  1450. int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
  1451. struct snd_ctl_elem_value *ucontrol)
  1452. {
  1453. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1454. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1455. unsigned short reg_val, val, mux;
  1456. reg_val = snd_soc_read(codec, e->reg);
  1457. val = (reg_val >> e->shift_l) & e->mask;
  1458. for (mux = 0; mux < e->max; mux++) {
  1459. if (val == e->values[mux])
  1460. break;
  1461. }
  1462. ucontrol->value.enumerated.item[0] = mux;
  1463. if (e->shift_l != e->shift_r) {
  1464. val = (reg_val >> e->shift_r) & e->mask;
  1465. for (mux = 0; mux < e->max; mux++) {
  1466. if (val == e->values[mux])
  1467. break;
  1468. }
  1469. ucontrol->value.enumerated.item[1] = mux;
  1470. }
  1471. return 0;
  1472. }
  1473. EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
  1474. /**
  1475. * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
  1476. * @kcontrol: mixer control
  1477. * @ucontrol: control element information
  1478. *
  1479. * Callback to set the value of a double semi enumerated mixer.
  1480. *
  1481. * Semi enumerated mixer: the enumerated items are referred as values. Can be
  1482. * used for handling bitfield coded enumeration for example.
  1483. *
  1484. * Returns 0 for success.
  1485. */
  1486. int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
  1487. struct snd_ctl_elem_value *ucontrol)
  1488. {
  1489. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1490. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1491. unsigned short val;
  1492. unsigned short mask;
  1493. if (ucontrol->value.enumerated.item[0] > e->max - 1)
  1494. return -EINVAL;
  1495. val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
  1496. mask = e->mask << e->shift_l;
  1497. if (e->shift_l != e->shift_r) {
  1498. if (ucontrol->value.enumerated.item[1] > e->max - 1)
  1499. return -EINVAL;
  1500. val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
  1501. mask |= e->mask << e->shift_r;
  1502. }
  1503. return snd_soc_update_bits(codec, e->reg, mask, val);
  1504. }
  1505. EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
  1506. /**
  1507. * snd_soc_info_enum_ext - external enumerated single mixer info callback
  1508. * @kcontrol: mixer control
  1509. * @uinfo: control element information
  1510. *
  1511. * Callback to provide information about an external enumerated
  1512. * single mixer.
  1513. *
  1514. * Returns 0 for success.
  1515. */
  1516. int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
  1517. struct snd_ctl_elem_info *uinfo)
  1518. {
  1519. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1520. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  1521. uinfo->count = 1;
  1522. uinfo->value.enumerated.items = e->max;
  1523. if (uinfo->value.enumerated.item > e->max - 1)
  1524. uinfo->value.enumerated.item = e->max - 1;
  1525. strcpy(uinfo->value.enumerated.name,
  1526. e->texts[uinfo->value.enumerated.item]);
  1527. return 0;
  1528. }
  1529. EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
  1530. /**
  1531. * snd_soc_info_volsw_ext - external single mixer info callback
  1532. * @kcontrol: mixer control
  1533. * @uinfo: control element information
  1534. *
  1535. * Callback to provide information about a single external mixer control.
  1536. *
  1537. * Returns 0 for success.
  1538. */
  1539. int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
  1540. struct snd_ctl_elem_info *uinfo)
  1541. {
  1542. int max = kcontrol->private_value;
  1543. if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
  1544. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  1545. else
  1546. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1547. uinfo->count = 1;
  1548. uinfo->value.integer.min = 0;
  1549. uinfo->value.integer.max = max;
  1550. return 0;
  1551. }
  1552. EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
  1553. /**
  1554. * snd_soc_info_volsw - single mixer info callback
  1555. * @kcontrol: mixer control
  1556. * @uinfo: control element information
  1557. *
  1558. * Callback to provide information about a single mixer control.
  1559. *
  1560. * Returns 0 for success.
  1561. */
  1562. int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
  1563. struct snd_ctl_elem_info *uinfo)
  1564. {
  1565. struct soc_mixer_control *mc =
  1566. (struct soc_mixer_control *)kcontrol->private_value;
  1567. int max = mc->max;
  1568. unsigned int shift = mc->shift;
  1569. unsigned int rshift = mc->rshift;
  1570. if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
  1571. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  1572. else
  1573. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1574. uinfo->count = shift == rshift ? 1 : 2;
  1575. uinfo->value.integer.min = 0;
  1576. uinfo->value.integer.max = max;
  1577. return 0;
  1578. }
  1579. EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
  1580. /**
  1581. * snd_soc_get_volsw - single mixer get callback
  1582. * @kcontrol: mixer control
  1583. * @ucontrol: control element information
  1584. *
  1585. * Callback to get the value of a single mixer control.
  1586. *
  1587. * Returns 0 for success.
  1588. */
  1589. int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
  1590. struct snd_ctl_elem_value *ucontrol)
  1591. {
  1592. struct soc_mixer_control *mc =
  1593. (struct soc_mixer_control *)kcontrol->private_value;
  1594. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1595. unsigned int reg = mc->reg;
  1596. unsigned int shift = mc->shift;
  1597. unsigned int rshift = mc->rshift;
  1598. int max = mc->max;
  1599. unsigned int mask = (1 << fls(max)) - 1;
  1600. unsigned int invert = mc->invert;
  1601. ucontrol->value.integer.value[0] =
  1602. (snd_soc_read(codec, reg) >> shift) & mask;
  1603. if (shift != rshift)
  1604. ucontrol->value.integer.value[1] =
  1605. (snd_soc_read(codec, reg) >> rshift) & mask;
  1606. if (invert) {
  1607. ucontrol->value.integer.value[0] =
  1608. max - ucontrol->value.integer.value[0];
  1609. if (shift != rshift)
  1610. ucontrol->value.integer.value[1] =
  1611. max - ucontrol->value.integer.value[1];
  1612. }
  1613. return 0;
  1614. }
  1615. EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
  1616. /**
  1617. * snd_soc_put_volsw - single mixer put callback
  1618. * @kcontrol: mixer control
  1619. * @ucontrol: control element information
  1620. *
  1621. * Callback to set the value of a single mixer control.
  1622. *
  1623. * Returns 0 for success.
  1624. */
  1625. int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
  1626. struct snd_ctl_elem_value *ucontrol)
  1627. {
  1628. struct soc_mixer_control *mc =
  1629. (struct soc_mixer_control *)kcontrol->private_value;
  1630. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1631. unsigned int reg = mc->reg;
  1632. unsigned int shift = mc->shift;
  1633. unsigned int rshift = mc->rshift;
  1634. int max = mc->max;
  1635. unsigned int mask = (1 << fls(max)) - 1;
  1636. unsigned int invert = mc->invert;
  1637. unsigned short val, val2, val_mask;
  1638. val = (ucontrol->value.integer.value[0] & mask);
  1639. if (invert)
  1640. val = max - val;
  1641. val_mask = mask << shift;
  1642. val = val << shift;
  1643. if (shift != rshift) {
  1644. val2 = (ucontrol->value.integer.value[1] & mask);
  1645. if (invert)
  1646. val2 = max - val2;
  1647. val_mask |= mask << rshift;
  1648. val |= val2 << rshift;
  1649. }
  1650. return snd_soc_update_bits(codec, reg, val_mask, val);
  1651. }
  1652. EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
  1653. /**
  1654. * snd_soc_info_volsw_2r - double mixer info callback
  1655. * @kcontrol: mixer control
  1656. * @uinfo: control element information
  1657. *
  1658. * Callback to provide information about a double mixer control that
  1659. * spans 2 codec registers.
  1660. *
  1661. * Returns 0 for success.
  1662. */
  1663. int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
  1664. struct snd_ctl_elem_info *uinfo)
  1665. {
  1666. struct soc_mixer_control *mc =
  1667. (struct soc_mixer_control *)kcontrol->private_value;
  1668. int max = mc->max;
  1669. if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
  1670. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  1671. else
  1672. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1673. uinfo->count = 2;
  1674. uinfo->value.integer.min = 0;
  1675. uinfo->value.integer.max = max;
  1676. return 0;
  1677. }
  1678. EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
  1679. /**
  1680. * snd_soc_get_volsw_2r - double mixer get callback
  1681. * @kcontrol: mixer control
  1682. * @ucontrol: control element information
  1683. *
  1684. * Callback to get the value of a double mixer control that spans 2 registers.
  1685. *
  1686. * Returns 0 for success.
  1687. */
  1688. int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
  1689. struct snd_ctl_elem_value *ucontrol)
  1690. {
  1691. struct soc_mixer_control *mc =
  1692. (struct soc_mixer_control *)kcontrol->private_value;
  1693. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1694. unsigned int reg = mc->reg;
  1695. unsigned int reg2 = mc->rreg;
  1696. unsigned int shift = mc->shift;
  1697. int max = mc->max;
  1698. unsigned int mask = (1<<fls(max))-1;
  1699. unsigned int invert = mc->invert;
  1700. ucontrol->value.integer.value[0] =
  1701. (snd_soc_read(codec, reg) >> shift) & mask;
  1702. ucontrol->value.integer.value[1] =
  1703. (snd_soc_read(codec, reg2) >> shift) & mask;
  1704. if (invert) {
  1705. ucontrol->value.integer.value[0] =
  1706. max - ucontrol->value.integer.value[0];
  1707. ucontrol->value.integer.value[1] =
  1708. max - ucontrol->value.integer.value[1];
  1709. }
  1710. return 0;
  1711. }
  1712. EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
  1713. /**
  1714. * snd_soc_put_volsw_2r - double mixer set callback
  1715. * @kcontrol: mixer control
  1716. * @ucontrol: control element information
  1717. *
  1718. * Callback to set the value of a double mixer control that spans 2 registers.
  1719. *
  1720. * Returns 0 for success.
  1721. */
  1722. int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
  1723. struct snd_ctl_elem_value *ucontrol)
  1724. {
  1725. struct soc_mixer_control *mc =
  1726. (struct soc_mixer_control *)kcontrol->private_value;
  1727. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1728. unsigned int reg = mc->reg;
  1729. unsigned int reg2 = mc->rreg;
  1730. unsigned int shift = mc->shift;
  1731. int max = mc->max;
  1732. unsigned int mask = (1 << fls(max)) - 1;
  1733. unsigned int invert = mc->invert;
  1734. int err;
  1735. unsigned short val, val2, val_mask;
  1736. val_mask = mask << shift;
  1737. val = (ucontrol->value.integer.value[0] & mask);
  1738. val2 = (ucontrol->value.integer.value[1] & mask);
  1739. if (invert) {
  1740. val = max - val;
  1741. val2 = max - val2;
  1742. }
  1743. val = val << shift;
  1744. val2 = val2 << shift;
  1745. err = snd_soc_update_bits(codec, reg, val_mask, val);
  1746. if (err < 0)
  1747. return err;
  1748. err = snd_soc_update_bits(codec, reg2, val_mask, val2);
  1749. return err;
  1750. }
  1751. EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
  1752. /**
  1753. * snd_soc_info_volsw_s8 - signed mixer info callback
  1754. * @kcontrol: mixer control
  1755. * @uinfo: control element information
  1756. *
  1757. * Callback to provide information about a signed mixer control.
  1758. *
  1759. * Returns 0 for success.
  1760. */
  1761. int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
  1762. struct snd_ctl_elem_info *uinfo)
  1763. {
  1764. struct soc_mixer_control *mc =
  1765. (struct soc_mixer_control *)kcontrol->private_value;
  1766. int max = mc->max;
  1767. int min = mc->min;
  1768. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1769. uinfo->count = 2;
  1770. uinfo->value.integer.min = 0;
  1771. uinfo->value.integer.max = max-min;
  1772. return 0;
  1773. }
  1774. EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
  1775. /**
  1776. * snd_soc_get_volsw_s8 - signed mixer get callback
  1777. * @kcontrol: mixer control
  1778. * @ucontrol: control element information
  1779. *
  1780. * Callback to get the value of a signed mixer control.
  1781. *
  1782. * Returns 0 for success.
  1783. */
  1784. int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
  1785. struct snd_ctl_elem_value *ucontrol)
  1786. {
  1787. struct soc_mixer_control *mc =
  1788. (struct soc_mixer_control *)kcontrol->private_value;
  1789. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1790. unsigned int reg = mc->reg;
  1791. int min = mc->min;
  1792. int val = snd_soc_read(codec, reg);
  1793. ucontrol->value.integer.value[0] =
  1794. ((signed char)(val & 0xff))-min;
  1795. ucontrol->value.integer.value[1] =
  1796. ((signed char)((val >> 8) & 0xff))-min;
  1797. return 0;
  1798. }
  1799. EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
  1800. /**
  1801. * snd_soc_put_volsw_sgn - signed mixer put callback
  1802. * @kcontrol: mixer control
  1803. * @ucontrol: control element information
  1804. *
  1805. * Callback to set the value of a signed mixer control.
  1806. *
  1807. * Returns 0 for success.
  1808. */
  1809. int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
  1810. struct snd_ctl_elem_value *ucontrol)
  1811. {
  1812. struct soc_mixer_control *mc =
  1813. (struct soc_mixer_control *)kcontrol->private_value;
  1814. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1815. unsigned int reg = mc->reg;
  1816. int min = mc->min;
  1817. unsigned short val;
  1818. val = (ucontrol->value.integer.value[0]+min) & 0xff;
  1819. val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
  1820. return snd_soc_update_bits(codec, reg, 0xffff, val);
  1821. }
  1822. EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
  1823. /**
  1824. * snd_soc_dai_set_sysclk - configure DAI system or master clock.
  1825. * @dai: DAI
  1826. * @clk_id: DAI specific clock ID
  1827. * @freq: new clock frequency in Hz
  1828. * @dir: new clock direction - input/output.
  1829. *
  1830. * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
  1831. */
  1832. int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
  1833. unsigned int freq, int dir)
  1834. {
  1835. if (dai->ops && dai->ops->set_sysclk)
  1836. return dai->ops->set_sysclk(dai, clk_id, freq, dir);
  1837. else
  1838. return -EINVAL;
  1839. }
  1840. EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
  1841. /**
  1842. * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
  1843. * @dai: DAI
  1844. * @div_id: DAI specific clock divider ID
  1845. * @div: new clock divisor.
  1846. *
  1847. * Configures the clock dividers. This is used to derive the best DAI bit and
  1848. * frame clocks from the system or master clock. It's best to set the DAI bit
  1849. * and frame clocks as low as possible to save system power.
  1850. */
  1851. int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
  1852. int div_id, int div)
  1853. {
  1854. if (dai->ops && dai->ops->set_clkdiv)
  1855. return dai->ops->set_clkdiv(dai, div_id, div);
  1856. else
  1857. return -EINVAL;
  1858. }
  1859. EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
  1860. /**
  1861. * snd_soc_dai_set_pll - configure DAI PLL.
  1862. * @dai: DAI
  1863. * @pll_id: DAI specific PLL ID
  1864. * @freq_in: PLL input clock frequency in Hz
  1865. * @freq_out: requested PLL output clock frequency in Hz
  1866. *
  1867. * Configures and enables PLL to generate output clock based on input clock.
  1868. */
  1869. int snd_soc_dai_set_pll(struct snd_soc_dai *dai,
  1870. int pll_id, unsigned int freq_in, unsigned int freq_out)
  1871. {
  1872. if (dai->ops && dai->ops->set_pll)
  1873. return dai->ops->set_pll(dai, pll_id, freq_in, freq_out);
  1874. else
  1875. return -EINVAL;
  1876. }
  1877. EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
  1878. /**
  1879. * snd_soc_dai_set_fmt - configure DAI hardware audio format.
  1880. * @dai: DAI
  1881. * @fmt: SND_SOC_DAIFMT_ format value.
  1882. *
  1883. * Configures the DAI hardware format and clocking.
  1884. */
  1885. int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  1886. {
  1887. if (dai->ops && dai->ops->set_fmt)
  1888. return dai->ops->set_fmt(dai, fmt);
  1889. else
  1890. return -EINVAL;
  1891. }
  1892. EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
  1893. /**
  1894. * snd_soc_dai_set_tdm_slot - configure DAI TDM.
  1895. * @dai: DAI
  1896. * @mask: DAI specific mask representing used slots.
  1897. * @slots: Number of slots in use.
  1898. *
  1899. * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
  1900. * specific.
  1901. */
  1902. int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
  1903. unsigned int mask, int slots)
  1904. {
  1905. if (dai->ops && dai->ops->set_tdm_slot)
  1906. return dai->ops->set_tdm_slot(dai, mask, slots);
  1907. else
  1908. return -EINVAL;
  1909. }
  1910. EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
  1911. /**
  1912. * snd_soc_dai_set_tristate - configure DAI system or master clock.
  1913. * @dai: DAI
  1914. * @tristate: tristate enable
  1915. *
  1916. * Tristates the DAI so that others can use it.
  1917. */
  1918. int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
  1919. {
  1920. if (dai->ops && dai->ops->set_tristate)
  1921. return dai->ops->set_tristate(dai, tristate);
  1922. else
  1923. return -EINVAL;
  1924. }
  1925. EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
  1926. /**
  1927. * snd_soc_dai_digital_mute - configure DAI system or master clock.
  1928. * @dai: DAI
  1929. * @mute: mute enable
  1930. *
  1931. * Mutes the DAI DAC.
  1932. */
  1933. int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
  1934. {
  1935. if (dai->ops && dai->ops->digital_mute)
  1936. return dai->ops->digital_mute(dai, mute);
  1937. else
  1938. return -EINVAL;
  1939. }
  1940. EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
  1941. /**
  1942. * snd_soc_register_card - Register a card with the ASoC core
  1943. *
  1944. * @card: Card to register
  1945. *
  1946. * Note that currently this is an internal only function: it will be
  1947. * exposed to machine drivers after further backporting of ASoC v2
  1948. * registration APIs.
  1949. */
  1950. static int snd_soc_register_card(struct snd_soc_card *card)
  1951. {
  1952. if (!card->name || !card->dev)
  1953. return -EINVAL;
  1954. INIT_LIST_HEAD(&card->list);
  1955. card->instantiated = 0;
  1956. mutex_lock(&client_mutex);
  1957. list_add(&card->list, &card_list);
  1958. snd_soc_instantiate_cards();
  1959. mutex_unlock(&client_mutex);
  1960. dev_dbg(card->dev, "Registered card '%s'\n", card->name);
  1961. return 0;
  1962. }
  1963. /**
  1964. * snd_soc_unregister_card - Unregister a card with the ASoC core
  1965. *
  1966. * @card: Card to unregister
  1967. *
  1968. * Note that currently this is an internal only function: it will be
  1969. * exposed to machine drivers after further backporting of ASoC v2
  1970. * registration APIs.
  1971. */
  1972. static int snd_soc_unregister_card(struct snd_soc_card *card)
  1973. {
  1974. mutex_lock(&client_mutex);
  1975. list_del(&card->list);
  1976. mutex_unlock(&client_mutex);
  1977. dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
  1978. return 0;
  1979. }
  1980. static struct snd_soc_dai_ops null_dai_ops = {
  1981. };
  1982. /**
  1983. * snd_soc_register_dai - Register a DAI with the ASoC core
  1984. *
  1985. * @dai: DAI to register
  1986. */
  1987. int snd_soc_register_dai(struct snd_soc_dai *dai)
  1988. {
  1989. if (!dai->name)
  1990. return -EINVAL;
  1991. /* The device should become mandatory over time */
  1992. if (!dai->dev)
  1993. printk(KERN_WARNING "No device for DAI %s\n", dai->name);
  1994. if (!dai->ops)
  1995. dai->ops = &null_dai_ops;
  1996. INIT_LIST_HEAD(&dai->list);
  1997. mutex_lock(&client_mutex);
  1998. list_add(&dai->list, &dai_list);
  1999. snd_soc_instantiate_cards();
  2000. mutex_unlock(&client_mutex);
  2001. pr_debug("Registered DAI '%s'\n", dai->name);
  2002. return 0;
  2003. }
  2004. EXPORT_SYMBOL_GPL(snd_soc_register_dai);
  2005. /**
  2006. * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
  2007. *
  2008. * @dai: DAI to unregister
  2009. */
  2010. void snd_soc_unregister_dai(struct snd_soc_dai *dai)
  2011. {
  2012. mutex_lock(&client_mutex);
  2013. list_del(&dai->list);
  2014. mutex_unlock(&client_mutex);
  2015. pr_debug("Unregistered DAI '%s'\n", dai->name);
  2016. }
  2017. EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
  2018. /**
  2019. * snd_soc_register_dais - Register multiple DAIs with the ASoC core
  2020. *
  2021. * @dai: Array of DAIs to register
  2022. * @count: Number of DAIs
  2023. */
  2024. int snd_soc_register_dais(struct snd_soc_dai *dai, size_t count)
  2025. {
  2026. int i, ret;
  2027. for (i = 0; i < count; i++) {
  2028. ret = snd_soc_register_dai(&dai[i]);
  2029. if (ret != 0)
  2030. goto err;
  2031. }
  2032. return 0;
  2033. err:
  2034. for (i--; i >= 0; i--)
  2035. snd_soc_unregister_dai(&dai[i]);
  2036. return ret;
  2037. }
  2038. EXPORT_SYMBOL_GPL(snd_soc_register_dais);
  2039. /**
  2040. * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
  2041. *
  2042. * @dai: Array of DAIs to unregister
  2043. * @count: Number of DAIs
  2044. */
  2045. void snd_soc_unregister_dais(struct snd_soc_dai *dai, size_t count)
  2046. {
  2047. int i;
  2048. for (i = 0; i < count; i++)
  2049. snd_soc_unregister_dai(&dai[i]);
  2050. }
  2051. EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
  2052. /**
  2053. * snd_soc_register_platform - Register a platform with the ASoC core
  2054. *
  2055. * @platform: platform to register
  2056. */
  2057. int snd_soc_register_platform(struct snd_soc_platform *platform)
  2058. {
  2059. if (!platform->name)
  2060. return -EINVAL;
  2061. INIT_LIST_HEAD(&platform->list);
  2062. mutex_lock(&client_mutex);
  2063. list_add(&platform->list, &platform_list);
  2064. snd_soc_instantiate_cards();
  2065. mutex_unlock(&client_mutex);
  2066. pr_debug("Registered platform '%s'\n", platform->name);
  2067. return 0;
  2068. }
  2069. EXPORT_SYMBOL_GPL(snd_soc_register_platform);
  2070. /**
  2071. * snd_soc_unregister_platform - Unregister a platform from the ASoC core
  2072. *
  2073. * @platform: platform to unregister
  2074. */
  2075. void snd_soc_unregister_platform(struct snd_soc_platform *platform)
  2076. {
  2077. mutex_lock(&client_mutex);
  2078. list_del(&platform->list);
  2079. mutex_unlock(&client_mutex);
  2080. pr_debug("Unregistered platform '%s'\n", platform->name);
  2081. }
  2082. EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
  2083. /**
  2084. * snd_soc_register_codec - Register a codec with the ASoC core
  2085. *
  2086. * @codec: codec to register
  2087. */
  2088. int snd_soc_register_codec(struct snd_soc_codec *codec)
  2089. {
  2090. if (!codec->name)
  2091. return -EINVAL;
  2092. /* The device should become mandatory over time */
  2093. if (!codec->dev)
  2094. printk(KERN_WARNING "No device for codec %s\n", codec->name);
  2095. INIT_LIST_HEAD(&codec->list);
  2096. mutex_lock(&client_mutex);
  2097. list_add(&codec->list, &codec_list);
  2098. snd_soc_instantiate_cards();
  2099. mutex_unlock(&client_mutex);
  2100. pr_debug("Registered codec '%s'\n", codec->name);
  2101. return 0;
  2102. }
  2103. EXPORT_SYMBOL_GPL(snd_soc_register_codec);
  2104. /**
  2105. * snd_soc_unregister_codec - Unregister a codec from the ASoC core
  2106. *
  2107. * @codec: codec to unregister
  2108. */
  2109. void snd_soc_unregister_codec(struct snd_soc_codec *codec)
  2110. {
  2111. mutex_lock(&client_mutex);
  2112. list_del(&codec->list);
  2113. mutex_unlock(&client_mutex);
  2114. pr_debug("Unregistered codec '%s'\n", codec->name);
  2115. }
  2116. EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
  2117. static int __init snd_soc_init(void)
  2118. {
  2119. #ifdef CONFIG_DEBUG_FS
  2120. debugfs_root = debugfs_create_dir("asoc", NULL);
  2121. if (IS_ERR(debugfs_root) || !debugfs_root) {
  2122. printk(KERN_WARNING
  2123. "ASoC: Failed to create debugfs directory\n");
  2124. debugfs_root = NULL;
  2125. }
  2126. #endif
  2127. return platform_driver_register(&soc_driver);
  2128. }
  2129. static void __exit snd_soc_exit(void)
  2130. {
  2131. #ifdef CONFIG_DEBUG_FS
  2132. debugfs_remove_recursive(debugfs_root);
  2133. #endif
  2134. platform_driver_unregister(&soc_driver);
  2135. }
  2136. module_init(snd_soc_init);
  2137. module_exit(snd_soc_exit);
  2138. /* Module information */
  2139. MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
  2140. MODULE_DESCRIPTION("ALSA SoC Core");
  2141. MODULE_LICENSE("GPL");
  2142. MODULE_ALIAS("platform:soc-audio");