soc-core.c 66 KB

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