soc-core.c 68 KB

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