soc-core.c 59 KB

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