soc-core.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603
  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
  8. * liam.girdwood@wolfsonmicro.com or linux@wolfsonmicro.com
  9. * with code, comments and ideas from :-
  10. * Richard Purdie <richard@openedhand.com>
  11. *
  12. * This program is free software; you can redistribute it and/or modify it
  13. * under the terms of the GNU General Public License as published by the
  14. * Free Software Foundation; either version 2 of the License, or (at your
  15. * option) any later version.
  16. *
  17. * Revision history
  18. * 12th Aug 2005 Initial version.
  19. * 25th Oct 2005 Working Codec, Interface and Platform registration.
  20. *
  21. * TODO:
  22. * o Add hw rules to enforce rates, etc.
  23. * o More testing with other codecs/machines.
  24. * o Add more codecs and platforms to ensure good API coverage.
  25. * o Support TDM on PCM and I2S
  26. */
  27. #include <linux/module.h>
  28. #include <linux/moduleparam.h>
  29. #include <linux/init.h>
  30. #include <linux/delay.h>
  31. #include <linux/pm.h>
  32. #include <linux/bitops.h>
  33. #include <linux/platform_device.h>
  34. #include <sound/core.h>
  35. #include <sound/pcm.h>
  36. #include <sound/pcm_params.h>
  37. #include <sound/soc.h>
  38. #include <sound/soc-dapm.h>
  39. #include <sound/initval.h>
  40. /* debug */
  41. #define SOC_DEBUG 0
  42. #if SOC_DEBUG
  43. #define dbg(format, arg...) printk(format, ## arg)
  44. #else
  45. #define dbg(format, arg...)
  46. #endif
  47. static DEFINE_MUTEX(pcm_mutex);
  48. static DEFINE_MUTEX(io_mutex);
  49. static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
  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 = NULL;
  90. codec->ac97->dev.release = soc_ac97_device_release;
  91. snprintf(codec->ac97->dev.bus_id, BUS_ID_SIZE, "%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 inline const char* get_dai_name(int type)
  103. {
  104. switch(type) {
  105. case SND_SOC_DAI_AC97_BUS:
  106. case SND_SOC_DAI_AC97:
  107. return "AC97";
  108. case SND_SOC_DAI_I2S:
  109. return "I2S";
  110. case SND_SOC_DAI_PCM:
  111. return "PCM";
  112. }
  113. return NULL;
  114. }
  115. /*
  116. * Called by ALSA when a PCM substream is opened, the runtime->hw record is
  117. * then initialized and any private data can be allocated. This also calls
  118. * startup for the cpu DAI, platform, machine and codec DAI.
  119. */
  120. static int soc_pcm_open(struct snd_pcm_substream *substream)
  121. {
  122. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  123. struct snd_soc_device *socdev = rtd->socdev;
  124. struct snd_pcm_runtime *runtime = substream->runtime;
  125. struct snd_soc_dai_link *machine = rtd->dai;
  126. struct snd_soc_platform *platform = socdev->platform;
  127. struct snd_soc_cpu_dai *cpu_dai = machine->cpu_dai;
  128. struct snd_soc_codec_dai *codec_dai = machine->codec_dai;
  129. int ret = 0;
  130. mutex_lock(&pcm_mutex);
  131. /* startup the audio subsystem */
  132. if (cpu_dai->ops.startup) {
  133. ret = cpu_dai->ops.startup(substream);
  134. if (ret < 0) {
  135. printk(KERN_ERR "asoc: can't open interface %s\n",
  136. cpu_dai->name);
  137. goto out;
  138. }
  139. }
  140. if (platform->pcm_ops->open) {
  141. ret = platform->pcm_ops->open(substream);
  142. if (ret < 0) {
  143. printk(KERN_ERR "asoc: can't open platform %s\n", platform->name);
  144. goto platform_err;
  145. }
  146. }
  147. if (codec_dai->ops.startup) {
  148. ret = codec_dai->ops.startup(substream);
  149. if (ret < 0) {
  150. printk(KERN_ERR "asoc: can't open codec %s\n",
  151. codec_dai->name);
  152. goto codec_dai_err;
  153. }
  154. }
  155. if (machine->ops && machine->ops->startup) {
  156. ret = machine->ops->startup(substream);
  157. if (ret < 0) {
  158. printk(KERN_ERR "asoc: %s startup failed\n", machine->name);
  159. goto machine_err;
  160. }
  161. }
  162. /* Check that the codec and cpu DAI's are compatible */
  163. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  164. runtime->hw.rate_min =
  165. max(codec_dai->playback.rate_min, cpu_dai->playback.rate_min);
  166. runtime->hw.rate_max =
  167. min(codec_dai->playback.rate_max, cpu_dai->playback.rate_max);
  168. runtime->hw.channels_min =
  169. max(codec_dai->playback.channels_min,
  170. cpu_dai->playback.channels_min);
  171. runtime->hw.channels_max =
  172. min(codec_dai->playback.channels_max,
  173. cpu_dai->playback.channels_max);
  174. runtime->hw.formats =
  175. codec_dai->playback.formats & cpu_dai->playback.formats;
  176. runtime->hw.rates =
  177. codec_dai->playback.rates & cpu_dai->playback.rates;
  178. } else {
  179. runtime->hw.rate_min =
  180. max(codec_dai->capture.rate_min, cpu_dai->capture.rate_min);
  181. runtime->hw.rate_max =
  182. min(codec_dai->capture.rate_max, cpu_dai->capture.rate_max);
  183. runtime->hw.channels_min =
  184. max(codec_dai->capture.channels_min,
  185. cpu_dai->capture.channels_min);
  186. runtime->hw.channels_max =
  187. min(codec_dai->capture.channels_max,
  188. cpu_dai->capture.channels_max);
  189. runtime->hw.formats =
  190. codec_dai->capture.formats & cpu_dai->capture.formats;
  191. runtime->hw.rates =
  192. codec_dai->capture.rates & cpu_dai->capture.rates;
  193. }
  194. snd_pcm_limit_hw_rates(runtime);
  195. if (!runtime->hw.rates) {
  196. printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
  197. codec_dai->name, cpu_dai->name);
  198. goto machine_err;
  199. }
  200. if (!runtime->hw.formats) {
  201. printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
  202. codec_dai->name, cpu_dai->name);
  203. goto machine_err;
  204. }
  205. if (!runtime->hw.channels_min || !runtime->hw.channels_max) {
  206. printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
  207. codec_dai->name, cpu_dai->name);
  208. goto machine_err;
  209. }
  210. dbg("asoc: %s <-> %s info:\n",codec_dai->name, cpu_dai->name);
  211. dbg("asoc: rate mask 0x%x\n", runtime->hw.rates);
  212. dbg("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
  213. runtime->hw.channels_max);
  214. dbg("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
  215. runtime->hw.rate_max);
  216. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  217. cpu_dai->playback.active = codec_dai->playback.active = 1;
  218. else
  219. cpu_dai->capture.active = codec_dai->capture.active = 1;
  220. cpu_dai->active = codec_dai->active = 1;
  221. cpu_dai->runtime = runtime;
  222. socdev->codec->active++;
  223. mutex_unlock(&pcm_mutex);
  224. return 0;
  225. machine_err:
  226. if (machine->ops && machine->ops->shutdown)
  227. machine->ops->shutdown(substream);
  228. codec_dai_err:
  229. if (platform->pcm_ops->close)
  230. platform->pcm_ops->close(substream);
  231. platform_err:
  232. if (cpu_dai->ops.shutdown)
  233. cpu_dai->ops.shutdown(substream);
  234. out:
  235. mutex_unlock(&pcm_mutex);
  236. return ret;
  237. }
  238. /*
  239. * Power down the audio subsystem pmdown_time msecs after close is called.
  240. * This is to ensure there are no pops or clicks in between any music tracks
  241. * due to DAPM power cycling.
  242. */
  243. static void close_delayed_work(struct work_struct *work)
  244. {
  245. struct snd_soc_device *socdev =
  246. container_of(work, struct snd_soc_device, delayed_work.work);
  247. struct snd_soc_codec *codec = socdev->codec;
  248. struct snd_soc_codec_dai *codec_dai;
  249. int i;
  250. mutex_lock(&pcm_mutex);
  251. for(i = 0; i < codec->num_dai; i++) {
  252. codec_dai = &codec->dai[i];
  253. dbg("pop wq checking: %s status: %s waiting: %s\n",
  254. codec_dai->playback.stream_name,
  255. codec_dai->playback.active ? "active" : "inactive",
  256. codec_dai->pop_wait ? "yes" : "no");
  257. /* are we waiting on this codec DAI stream */
  258. if (codec_dai->pop_wait == 1) {
  259. /* power down the codec to D1 if no longer active */
  260. if (codec->active == 0) {
  261. dbg("pop wq D1 %s %s\n", codec->name,
  262. codec_dai->playback.stream_name);
  263. snd_soc_dapm_device_event(socdev,
  264. SNDRV_CTL_POWER_D1);
  265. }
  266. codec_dai->pop_wait = 0;
  267. snd_soc_dapm_stream_event(codec,
  268. codec_dai->playback.stream_name,
  269. SND_SOC_DAPM_STREAM_STOP);
  270. /* power down the codec power domain if no longer active */
  271. if (codec->active == 0) {
  272. dbg("pop wq D3 %s %s\n", codec->name,
  273. codec_dai->playback.stream_name);
  274. snd_soc_dapm_device_event(socdev,
  275. SNDRV_CTL_POWER_D3hot);
  276. }
  277. }
  278. }
  279. mutex_unlock(&pcm_mutex);
  280. }
  281. /*
  282. * Called by ALSA when a PCM substream is closed. Private data can be
  283. * freed here. The cpu DAI, codec DAI, machine and platform are also
  284. * shutdown.
  285. */
  286. static int soc_codec_close(struct snd_pcm_substream *substream)
  287. {
  288. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  289. struct snd_soc_device *socdev = rtd->socdev;
  290. struct snd_soc_dai_link *machine = rtd->dai;
  291. struct snd_soc_platform *platform = socdev->platform;
  292. struct snd_soc_cpu_dai *cpu_dai = machine->cpu_dai;
  293. struct snd_soc_codec_dai *codec_dai = machine->codec_dai;
  294. struct snd_soc_codec *codec = socdev->codec;
  295. mutex_lock(&pcm_mutex);
  296. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  297. cpu_dai->playback.active = codec_dai->playback.active = 0;
  298. else
  299. cpu_dai->capture.active = codec_dai->capture.active = 0;
  300. if (codec_dai->playback.active == 0 &&
  301. codec_dai->capture.active == 0) {
  302. cpu_dai->active = codec_dai->active = 0;
  303. }
  304. codec->active--;
  305. if (cpu_dai->ops.shutdown)
  306. cpu_dai->ops.shutdown(substream);
  307. if (codec_dai->ops.shutdown)
  308. codec_dai->ops.shutdown(substream);
  309. if (machine->ops && machine->ops->shutdown)
  310. machine->ops->shutdown(substream);
  311. if (platform->pcm_ops->close)
  312. platform->pcm_ops->close(substream);
  313. cpu_dai->runtime = NULL;
  314. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  315. /* start delayed pop wq here for playback streams */
  316. codec_dai->pop_wait = 1;
  317. schedule_delayed_work(&socdev->delayed_work,
  318. msecs_to_jiffies(pmdown_time));
  319. } else {
  320. /* capture streams can be powered down now */
  321. snd_soc_dapm_stream_event(codec,
  322. codec_dai->capture.stream_name,
  323. SND_SOC_DAPM_STREAM_STOP);
  324. if (codec->active == 0 && codec_dai->pop_wait == 0)
  325. snd_soc_dapm_device_event(socdev,
  326. SNDRV_CTL_POWER_D3hot);
  327. }
  328. mutex_unlock(&pcm_mutex);
  329. return 0;
  330. }
  331. /*
  332. * Called by ALSA when the PCM substream is prepared, can set format, sample
  333. * rate, etc. This function is non atomic and can be called multiple times,
  334. * it can refer to the runtime info.
  335. */
  336. static int soc_pcm_prepare(struct snd_pcm_substream *substream)
  337. {
  338. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  339. struct snd_soc_device *socdev = rtd->socdev;
  340. struct snd_soc_dai_link *machine = rtd->dai;
  341. struct snd_soc_platform *platform = socdev->platform;
  342. struct snd_soc_cpu_dai *cpu_dai = machine->cpu_dai;
  343. struct snd_soc_codec_dai *codec_dai = machine->codec_dai;
  344. struct snd_soc_codec *codec = socdev->codec;
  345. int ret = 0;
  346. mutex_lock(&pcm_mutex);
  347. if (machine->ops && machine->ops->prepare) {
  348. ret = machine->ops->prepare(substream);
  349. if (ret < 0) {
  350. printk(KERN_ERR "asoc: machine prepare error\n");
  351. goto out;
  352. }
  353. }
  354. if (platform->pcm_ops->prepare) {
  355. ret = platform->pcm_ops->prepare(substream);
  356. if (ret < 0) {
  357. printk(KERN_ERR "asoc: platform prepare error\n");
  358. goto out;
  359. }
  360. }
  361. if (codec_dai->ops.prepare) {
  362. ret = codec_dai->ops.prepare(substream);
  363. if (ret < 0) {
  364. printk(KERN_ERR "asoc: codec DAI prepare error\n");
  365. goto out;
  366. }
  367. }
  368. if (cpu_dai->ops.prepare) {
  369. ret = cpu_dai->ops.prepare(substream);
  370. if (ret < 0) {
  371. printk(KERN_ERR "asoc: cpu DAI prepare error\n");
  372. goto out;
  373. }
  374. }
  375. /* we only want to start a DAPM playback stream if we are not waiting
  376. * on an existing one stopping */
  377. if (codec_dai->pop_wait) {
  378. /* we are waiting for the delayed work to start */
  379. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  380. snd_soc_dapm_stream_event(socdev->codec,
  381. codec_dai->capture.stream_name,
  382. SND_SOC_DAPM_STREAM_START);
  383. else {
  384. codec_dai->pop_wait = 0;
  385. cancel_delayed_work(&socdev->delayed_work);
  386. if (codec_dai->dai_ops.digital_mute)
  387. codec_dai->dai_ops.digital_mute(codec_dai, 0);
  388. }
  389. } else {
  390. /* no delayed work - do we need to power up codec */
  391. if (codec->dapm_state != SNDRV_CTL_POWER_D0) {
  392. snd_soc_dapm_device_event(socdev, SNDRV_CTL_POWER_D1);
  393. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  394. snd_soc_dapm_stream_event(codec,
  395. codec_dai->playback.stream_name,
  396. SND_SOC_DAPM_STREAM_START);
  397. else
  398. snd_soc_dapm_stream_event(codec,
  399. codec_dai->capture.stream_name,
  400. SND_SOC_DAPM_STREAM_START);
  401. snd_soc_dapm_device_event(socdev, SNDRV_CTL_POWER_D0);
  402. if (codec_dai->dai_ops.digital_mute)
  403. codec_dai->dai_ops.digital_mute(codec_dai, 0);
  404. } else {
  405. /* codec already powered - power on widgets */
  406. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  407. snd_soc_dapm_stream_event(codec,
  408. codec_dai->playback.stream_name,
  409. SND_SOC_DAPM_STREAM_START);
  410. else
  411. snd_soc_dapm_stream_event(codec,
  412. codec_dai->capture.stream_name,
  413. SND_SOC_DAPM_STREAM_START);
  414. if (codec_dai->dai_ops.digital_mute)
  415. codec_dai->dai_ops.digital_mute(codec_dai, 0);
  416. }
  417. }
  418. out:
  419. mutex_unlock(&pcm_mutex);
  420. return ret;
  421. }
  422. /*
  423. * Called by ALSA when the hardware params are set by application. This
  424. * function can also be called multiple times and can allocate buffers
  425. * (using snd_pcm_lib_* ). It's non-atomic.
  426. */
  427. static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
  428. struct snd_pcm_hw_params *params)
  429. {
  430. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  431. struct snd_soc_device *socdev = rtd->socdev;
  432. struct snd_soc_dai_link *machine = rtd->dai;
  433. struct snd_soc_platform *platform = socdev->platform;
  434. struct snd_soc_cpu_dai *cpu_dai = machine->cpu_dai;
  435. struct snd_soc_codec_dai *codec_dai = machine->codec_dai;
  436. int ret = 0;
  437. mutex_lock(&pcm_mutex);
  438. if (machine->ops && machine->ops->hw_params) {
  439. ret = machine->ops->hw_params(substream, params);
  440. if (ret < 0) {
  441. printk(KERN_ERR "asoc: machine hw_params failed\n");
  442. goto out;
  443. }
  444. }
  445. if (codec_dai->ops.hw_params) {
  446. ret = codec_dai->ops.hw_params(substream, params);
  447. if (ret < 0) {
  448. printk(KERN_ERR "asoc: can't set codec %s hw params\n",
  449. codec_dai->name);
  450. goto codec_err;
  451. }
  452. }
  453. if (cpu_dai->ops.hw_params) {
  454. ret = cpu_dai->ops.hw_params(substream, params);
  455. if (ret < 0) {
  456. printk(KERN_ERR "asoc: can't set interface %s hw params\n",
  457. cpu_dai->name);
  458. goto interface_err;
  459. }
  460. }
  461. if (platform->pcm_ops->hw_params) {
  462. ret = platform->pcm_ops->hw_params(substream, params);
  463. if (ret < 0) {
  464. printk(KERN_ERR "asoc: can't set platform %s hw params\n",
  465. platform->name);
  466. goto platform_err;
  467. }
  468. }
  469. out:
  470. mutex_unlock(&pcm_mutex);
  471. return ret;
  472. platform_err:
  473. if (cpu_dai->ops.hw_free)
  474. cpu_dai->ops.hw_free(substream);
  475. interface_err:
  476. if (codec_dai->ops.hw_free)
  477. codec_dai->ops.hw_free(substream);
  478. codec_err:
  479. if(machine->ops && machine->ops->hw_free)
  480. machine->ops->hw_free(substream);
  481. mutex_unlock(&pcm_mutex);
  482. return ret;
  483. }
  484. /*
  485. * Free's resources allocated by hw_params, can be called multiple times
  486. */
  487. static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
  488. {
  489. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  490. struct snd_soc_device *socdev = rtd->socdev;
  491. struct snd_soc_dai_link *machine = rtd->dai;
  492. struct snd_soc_platform *platform = socdev->platform;
  493. struct snd_soc_cpu_dai *cpu_dai = machine->cpu_dai;
  494. struct snd_soc_codec_dai *codec_dai = machine->codec_dai;
  495. struct snd_soc_codec *codec = socdev->codec;
  496. mutex_lock(&pcm_mutex);
  497. /* apply codec digital mute */
  498. if (!codec->active && codec_dai->dai_ops.digital_mute)
  499. codec_dai->dai_ops.digital_mute(codec_dai, 1);
  500. /* free any machine hw params */
  501. if (machine->ops && machine->ops->hw_free)
  502. machine->ops->hw_free(substream);
  503. /* free any DMA resources */
  504. if (platform->pcm_ops->hw_free)
  505. platform->pcm_ops->hw_free(substream);
  506. /* now free hw params for the DAI's */
  507. if (codec_dai->ops.hw_free)
  508. codec_dai->ops.hw_free(substream);
  509. if (cpu_dai->ops.hw_free)
  510. cpu_dai->ops.hw_free(substream);
  511. mutex_unlock(&pcm_mutex);
  512. return 0;
  513. }
  514. static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  515. {
  516. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  517. struct snd_soc_device *socdev = rtd->socdev;
  518. struct snd_soc_dai_link *machine = rtd->dai;
  519. struct snd_soc_platform *platform = socdev->platform;
  520. struct snd_soc_cpu_dai *cpu_dai = machine->cpu_dai;
  521. struct snd_soc_codec_dai *codec_dai = machine->codec_dai;
  522. int ret;
  523. if (codec_dai->ops.trigger) {
  524. ret = codec_dai->ops.trigger(substream, cmd);
  525. if (ret < 0)
  526. return ret;
  527. }
  528. if (platform->pcm_ops->trigger) {
  529. ret = platform->pcm_ops->trigger(substream, cmd);
  530. if (ret < 0)
  531. return ret;
  532. }
  533. if (cpu_dai->ops.trigger) {
  534. ret = cpu_dai->ops.trigger(substream, cmd);
  535. if (ret < 0)
  536. return ret;
  537. }
  538. return 0;
  539. }
  540. /* ASoC PCM operations */
  541. static struct snd_pcm_ops soc_pcm_ops = {
  542. .open = soc_pcm_open,
  543. .close = soc_codec_close,
  544. .hw_params = soc_pcm_hw_params,
  545. .hw_free = soc_pcm_hw_free,
  546. .prepare = soc_pcm_prepare,
  547. .trigger = soc_pcm_trigger,
  548. };
  549. #ifdef CONFIG_PM
  550. /* powers down audio subsystem for suspend */
  551. static int soc_suspend(struct platform_device *pdev, pm_message_t state)
  552. {
  553. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  554. struct snd_soc_machine *machine = socdev->machine;
  555. struct snd_soc_platform *platform = socdev->platform;
  556. struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
  557. struct snd_soc_codec *codec = socdev->codec;
  558. int i;
  559. /* mute any active DAC's */
  560. for(i = 0; i < machine->num_links; i++) {
  561. struct snd_soc_codec_dai *dai = machine->dai_link[i].codec_dai;
  562. if (dai->dai_ops.digital_mute && dai->playback.active)
  563. dai->dai_ops.digital_mute(dai, 1);
  564. }
  565. /* suspend all pcms */
  566. for (i = 0; i < machine->num_links; i++)
  567. snd_pcm_suspend_all(machine->dai_link[i].pcm);
  568. if (machine->suspend_pre)
  569. machine->suspend_pre(pdev, state);
  570. for(i = 0; i < machine->num_links; i++) {
  571. struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
  572. if (cpu_dai->suspend && cpu_dai->type != SND_SOC_DAI_AC97)
  573. cpu_dai->suspend(pdev, cpu_dai);
  574. if (platform->suspend)
  575. platform->suspend(pdev, cpu_dai);
  576. }
  577. /* close any waiting streams and save state */
  578. run_delayed_work(&socdev->delayed_work);
  579. codec->suspend_dapm_state = codec->dapm_state;
  580. for(i = 0; i < codec->num_dai; i++) {
  581. char *stream = codec->dai[i].playback.stream_name;
  582. if (stream != NULL)
  583. snd_soc_dapm_stream_event(codec, stream,
  584. SND_SOC_DAPM_STREAM_SUSPEND);
  585. stream = codec->dai[i].capture.stream_name;
  586. if (stream != NULL)
  587. snd_soc_dapm_stream_event(codec, stream,
  588. SND_SOC_DAPM_STREAM_SUSPEND);
  589. }
  590. if (codec_dev->suspend)
  591. codec_dev->suspend(pdev, state);
  592. for(i = 0; i < machine->num_links; i++) {
  593. struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
  594. if (cpu_dai->suspend && cpu_dai->type == SND_SOC_DAI_AC97)
  595. cpu_dai->suspend(pdev, cpu_dai);
  596. }
  597. if (machine->suspend_post)
  598. machine->suspend_post(pdev, state);
  599. return 0;
  600. }
  601. /* powers up audio subsystem after a suspend */
  602. static int soc_resume(struct platform_device *pdev)
  603. {
  604. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  605. struct snd_soc_machine *machine = socdev->machine;
  606. struct snd_soc_platform *platform = socdev->platform;
  607. struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
  608. struct snd_soc_codec *codec = socdev->codec;
  609. int i;
  610. if (machine->resume_pre)
  611. machine->resume_pre(pdev);
  612. for(i = 0; i < machine->num_links; i++) {
  613. struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
  614. if (cpu_dai->resume && cpu_dai->type == SND_SOC_DAI_AC97)
  615. cpu_dai->resume(pdev, cpu_dai);
  616. }
  617. if (codec_dev->resume)
  618. codec_dev->resume(pdev);
  619. for(i = 0; i < codec->num_dai; i++) {
  620. char* stream = codec->dai[i].playback.stream_name;
  621. if (stream != NULL)
  622. snd_soc_dapm_stream_event(codec, stream,
  623. SND_SOC_DAPM_STREAM_RESUME);
  624. stream = codec->dai[i].capture.stream_name;
  625. if (stream != NULL)
  626. snd_soc_dapm_stream_event(codec, stream,
  627. SND_SOC_DAPM_STREAM_RESUME);
  628. }
  629. /* unmute any active DAC's */
  630. for(i = 0; i < machine->num_links; i++) {
  631. struct snd_soc_codec_dai *dai = machine->dai_link[i].codec_dai;
  632. if (dai->dai_ops.digital_mute && dai->playback.active)
  633. dai->dai_ops.digital_mute(dai, 0);
  634. }
  635. for(i = 0; i < machine->num_links; i++) {
  636. struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
  637. if (cpu_dai->resume && cpu_dai->type != SND_SOC_DAI_AC97)
  638. cpu_dai->resume(pdev, cpu_dai);
  639. if (platform->resume)
  640. platform->resume(pdev, cpu_dai);
  641. }
  642. if (machine->resume_post)
  643. machine->resume_post(pdev);
  644. return 0;
  645. }
  646. #else
  647. #define soc_suspend NULL
  648. #define soc_resume NULL
  649. #endif
  650. /* probes a new socdev */
  651. static int soc_probe(struct platform_device *pdev)
  652. {
  653. int ret = 0, i;
  654. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  655. struct snd_soc_machine *machine = socdev->machine;
  656. struct snd_soc_platform *platform = socdev->platform;
  657. struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
  658. if (machine->probe) {
  659. ret = machine->probe(pdev);
  660. if(ret < 0)
  661. return ret;
  662. }
  663. for (i = 0; i < machine->num_links; i++) {
  664. struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
  665. if (cpu_dai->probe) {
  666. ret = cpu_dai->probe(pdev);
  667. if(ret < 0)
  668. goto cpu_dai_err;
  669. }
  670. }
  671. if (codec_dev->probe) {
  672. ret = codec_dev->probe(pdev);
  673. if(ret < 0)
  674. goto cpu_dai_err;
  675. }
  676. if (platform->probe) {
  677. ret = platform->probe(pdev);
  678. if(ret < 0)
  679. goto platform_err;
  680. }
  681. /* DAPM stream work */
  682. INIT_DELAYED_WORK(&socdev->delayed_work, close_delayed_work);
  683. return 0;
  684. platform_err:
  685. if (codec_dev->remove)
  686. codec_dev->remove(pdev);
  687. cpu_dai_err:
  688. for (i--; i >= 0; i--) {
  689. struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
  690. if (cpu_dai->remove)
  691. cpu_dai->remove(pdev);
  692. }
  693. if (machine->remove)
  694. machine->remove(pdev);
  695. return ret;
  696. }
  697. /* removes a socdev */
  698. static int soc_remove(struct platform_device *pdev)
  699. {
  700. int i;
  701. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  702. struct snd_soc_machine *machine = socdev->machine;
  703. struct snd_soc_platform *platform = socdev->platform;
  704. struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
  705. run_delayed_work(&socdev->delayed_work);
  706. if (platform->remove)
  707. platform->remove(pdev);
  708. if (codec_dev->remove)
  709. codec_dev->remove(pdev);
  710. for (i = 0; i < machine->num_links; i++) {
  711. struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
  712. if (cpu_dai->remove)
  713. cpu_dai->remove(pdev);
  714. }
  715. if (machine->remove)
  716. machine->remove(pdev);
  717. return 0;
  718. }
  719. /* ASoC platform driver */
  720. static struct platform_driver soc_driver = {
  721. .driver = {
  722. .name = "soc-audio",
  723. },
  724. .probe = soc_probe,
  725. .remove = soc_remove,
  726. .suspend = soc_suspend,
  727. .resume = soc_resume,
  728. };
  729. /* create a new pcm */
  730. static int soc_new_pcm(struct snd_soc_device *socdev,
  731. struct snd_soc_dai_link *dai_link, int num)
  732. {
  733. struct snd_soc_codec *codec = socdev->codec;
  734. struct snd_soc_codec_dai *codec_dai = dai_link->codec_dai;
  735. struct snd_soc_cpu_dai *cpu_dai = dai_link->cpu_dai;
  736. struct snd_soc_pcm_runtime *rtd;
  737. struct snd_pcm *pcm;
  738. char new_name[64];
  739. int ret = 0, playback = 0, capture = 0;
  740. rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
  741. if (rtd == NULL)
  742. return -ENOMEM;
  743. rtd->dai = dai_link;
  744. rtd->socdev = socdev;
  745. codec_dai->codec = socdev->codec;
  746. /* check client and interface hw capabilities */
  747. sprintf(new_name, "%s %s-%s-%d",dai_link->stream_name, codec_dai->name,
  748. get_dai_name(cpu_dai->type), num);
  749. if (codec_dai->playback.channels_min)
  750. playback = 1;
  751. if (codec_dai->capture.channels_min)
  752. capture = 1;
  753. ret = snd_pcm_new(codec->card, new_name, codec->pcm_devs++, playback,
  754. capture, &pcm);
  755. if (ret < 0) {
  756. printk(KERN_ERR "asoc: can't create pcm for codec %s\n", codec->name);
  757. kfree(rtd);
  758. return ret;
  759. }
  760. dai_link->pcm = pcm;
  761. pcm->private_data = rtd;
  762. soc_pcm_ops.mmap = socdev->platform->pcm_ops->mmap;
  763. soc_pcm_ops.pointer = socdev->platform->pcm_ops->pointer;
  764. soc_pcm_ops.ioctl = socdev->platform->pcm_ops->ioctl;
  765. soc_pcm_ops.copy = socdev->platform->pcm_ops->copy;
  766. soc_pcm_ops.silence = socdev->platform->pcm_ops->silence;
  767. soc_pcm_ops.ack = socdev->platform->pcm_ops->ack;
  768. soc_pcm_ops.page = socdev->platform->pcm_ops->page;
  769. if (playback)
  770. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
  771. if (capture)
  772. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
  773. ret = socdev->platform->pcm_new(codec->card, codec_dai, pcm);
  774. if (ret < 0) {
  775. printk(KERN_ERR "asoc: platform pcm constructor failed\n");
  776. kfree(rtd);
  777. return ret;
  778. }
  779. pcm->private_free = socdev->platform->pcm_free;
  780. printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
  781. cpu_dai->name);
  782. return ret;
  783. }
  784. /* codec register dump */
  785. static ssize_t codec_reg_show(struct device *dev,
  786. struct device_attribute *attr, char *buf)
  787. {
  788. struct snd_soc_device *devdata = dev_get_drvdata(dev);
  789. struct snd_soc_codec *codec = devdata->codec;
  790. int i, step = 1, count = 0;
  791. if (!codec->reg_cache_size)
  792. return 0;
  793. if (codec->reg_cache_step)
  794. step = codec->reg_cache_step;
  795. count += sprintf(buf, "%s registers\n", codec->name);
  796. for(i = 0; i < codec->reg_cache_size; i += step)
  797. count += sprintf(buf + count, "%2x: %4x\n", i, codec->read(codec, i));
  798. return count;
  799. }
  800. static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
  801. /**
  802. * snd_soc_new_ac97_codec - initailise AC97 device
  803. * @codec: audio codec
  804. * @ops: AC97 bus operations
  805. * @num: AC97 codec number
  806. *
  807. * Initialises AC97 codec resources for use by ad-hoc devices only.
  808. */
  809. int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
  810. struct snd_ac97_bus_ops *ops, int num)
  811. {
  812. mutex_lock(&codec->mutex);
  813. codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
  814. if (codec->ac97 == NULL) {
  815. mutex_unlock(&codec->mutex);
  816. return -ENOMEM;
  817. }
  818. codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
  819. if (codec->ac97->bus == NULL) {
  820. kfree(codec->ac97);
  821. codec->ac97 = NULL;
  822. mutex_unlock(&codec->mutex);
  823. return -ENOMEM;
  824. }
  825. codec->ac97->bus->ops = ops;
  826. codec->ac97->num = num;
  827. mutex_unlock(&codec->mutex);
  828. return 0;
  829. }
  830. EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
  831. /**
  832. * snd_soc_free_ac97_codec - free AC97 codec device
  833. * @codec: audio codec
  834. *
  835. * Frees AC97 codec device resources.
  836. */
  837. void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
  838. {
  839. mutex_lock(&codec->mutex);
  840. kfree(codec->ac97->bus);
  841. kfree(codec->ac97);
  842. codec->ac97 = NULL;
  843. mutex_unlock(&codec->mutex);
  844. }
  845. EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
  846. /**
  847. * snd_soc_update_bits - update codec register bits
  848. * @codec: audio codec
  849. * @reg: codec register
  850. * @mask: register mask
  851. * @value: new value
  852. *
  853. * Writes new register value.
  854. *
  855. * Returns 1 for change else 0.
  856. */
  857. int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
  858. unsigned short mask, unsigned short value)
  859. {
  860. int change;
  861. unsigned short old, new;
  862. mutex_lock(&io_mutex);
  863. old = snd_soc_read(codec, reg);
  864. new = (old & ~mask) | value;
  865. change = old != new;
  866. if (change)
  867. snd_soc_write(codec, reg, new);
  868. mutex_unlock(&io_mutex);
  869. return change;
  870. }
  871. EXPORT_SYMBOL_GPL(snd_soc_update_bits);
  872. /**
  873. * snd_soc_test_bits - test register for change
  874. * @codec: audio codec
  875. * @reg: codec register
  876. * @mask: register mask
  877. * @value: new value
  878. *
  879. * Tests a register with a new value and checks if the new value is
  880. * different from the old value.
  881. *
  882. * Returns 1 for change else 0.
  883. */
  884. int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
  885. unsigned short mask, unsigned short value)
  886. {
  887. int change;
  888. unsigned short old, new;
  889. mutex_lock(&io_mutex);
  890. old = snd_soc_read(codec, reg);
  891. new = (old & ~mask) | value;
  892. change = old != new;
  893. mutex_unlock(&io_mutex);
  894. return change;
  895. }
  896. EXPORT_SYMBOL_GPL(snd_soc_test_bits);
  897. /**
  898. * snd_soc_new_pcms - create new sound card and pcms
  899. * @socdev: the SoC audio device
  900. *
  901. * Create a new sound card based upon the codec and interface pcms.
  902. *
  903. * Returns 0 for success, else error.
  904. */
  905. int snd_soc_new_pcms(struct snd_soc_device *socdev, int idx, const char *xid)
  906. {
  907. struct snd_soc_codec *codec = socdev->codec;
  908. struct snd_soc_machine *machine = socdev->machine;
  909. int ret = 0, i;
  910. mutex_lock(&codec->mutex);
  911. /* register a sound card */
  912. codec->card = snd_card_new(idx, xid, codec->owner, 0);
  913. if (!codec->card) {
  914. printk(KERN_ERR "asoc: can't create sound card for codec %s\n",
  915. codec->name);
  916. mutex_unlock(&codec->mutex);
  917. return -ENODEV;
  918. }
  919. codec->card->dev = socdev->dev;
  920. codec->card->private_data = codec;
  921. strncpy(codec->card->driver, codec->name, sizeof(codec->card->driver));
  922. /* create the pcms */
  923. for(i = 0; i < machine->num_links; i++) {
  924. ret = soc_new_pcm(socdev, &machine->dai_link[i], i);
  925. if (ret < 0) {
  926. printk(KERN_ERR "asoc: can't create pcm %s\n",
  927. machine->dai_link[i].stream_name);
  928. mutex_unlock(&codec->mutex);
  929. return ret;
  930. }
  931. }
  932. mutex_unlock(&codec->mutex);
  933. return ret;
  934. }
  935. EXPORT_SYMBOL_GPL(snd_soc_new_pcms);
  936. /**
  937. * snd_soc_register_card - register sound card
  938. * @socdev: the SoC audio device
  939. *
  940. * Register a SoC sound card. Also registers an AC97 device if the
  941. * codec is AC97 for ad hoc devices.
  942. *
  943. * Returns 0 for success, else error.
  944. */
  945. int snd_soc_register_card(struct snd_soc_device *socdev)
  946. {
  947. struct snd_soc_codec *codec = socdev->codec;
  948. struct snd_soc_machine *machine = socdev->machine;
  949. int ret = 0, i, ac97 = 0, err = 0;
  950. for(i = 0; i < machine->num_links; i++) {
  951. if (socdev->machine->dai_link[i].init) {
  952. err = socdev->machine->dai_link[i].init(codec);
  953. if (err < 0) {
  954. printk(KERN_ERR "asoc: failed to init %s\n",
  955. socdev->machine->dai_link[i].stream_name);
  956. continue;
  957. }
  958. }
  959. if (socdev->machine->dai_link[i].codec_dai->type ==
  960. SND_SOC_DAI_AC97_BUS)
  961. ac97 = 1;
  962. }
  963. snprintf(codec->card->shortname, sizeof(codec->card->shortname),
  964. "%s", machine->name);
  965. snprintf(codec->card->longname, sizeof(codec->card->longname),
  966. "%s (%s)", machine->name, codec->name);
  967. ret = snd_card_register(codec->card);
  968. if (ret < 0) {
  969. printk(KERN_ERR "asoc: failed to register soundcard for codec %s\n",
  970. codec->name);
  971. goto out;
  972. }
  973. mutex_lock(&codec->mutex);
  974. #ifdef CONFIG_SND_SOC_AC97_BUS
  975. if (ac97) {
  976. ret = soc_ac97_dev_register(codec);
  977. if (ret < 0) {
  978. printk(KERN_ERR "asoc: AC97 device register failed\n");
  979. snd_card_free(codec->card);
  980. mutex_unlock(&codec->mutex);
  981. goto out;
  982. }
  983. }
  984. #endif
  985. err = snd_soc_dapm_sys_add(socdev->dev);
  986. if (err < 0)
  987. printk(KERN_WARNING "asoc: failed to add dapm sysfs entries\n");
  988. err = device_create_file(socdev->dev, &dev_attr_codec_reg);
  989. if (err < 0)
  990. printk(KERN_WARNING "asoc: failed to add codec sysfs entries\n");
  991. mutex_unlock(&codec->mutex);
  992. out:
  993. return ret;
  994. }
  995. EXPORT_SYMBOL_GPL(snd_soc_register_card);
  996. /**
  997. * snd_soc_free_pcms - free sound card and pcms
  998. * @socdev: the SoC audio device
  999. *
  1000. * Frees sound card and pcms associated with the socdev.
  1001. * Also unregister the codec if it is an AC97 device.
  1002. */
  1003. void snd_soc_free_pcms(struct snd_soc_device *socdev)
  1004. {
  1005. struct snd_soc_codec *codec = socdev->codec;
  1006. #ifdef CONFIG_SND_SOC_AC97_BUS
  1007. struct snd_soc_codec_dai *codec_dai;
  1008. int i;
  1009. #endif
  1010. mutex_lock(&codec->mutex);
  1011. #ifdef CONFIG_SND_SOC_AC97_BUS
  1012. for(i = 0; i < codec->num_dai; i++) {
  1013. codec_dai = &codec->dai[i];
  1014. if (codec_dai->type == SND_SOC_DAI_AC97_BUS && codec->ac97) {
  1015. soc_ac97_dev_unregister(codec);
  1016. goto free_card;
  1017. }
  1018. }
  1019. free_card:
  1020. #endif
  1021. if (codec->card)
  1022. snd_card_free(codec->card);
  1023. device_remove_file(socdev->dev, &dev_attr_codec_reg);
  1024. mutex_unlock(&codec->mutex);
  1025. }
  1026. EXPORT_SYMBOL_GPL(snd_soc_free_pcms);
  1027. /**
  1028. * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
  1029. * @substream: the pcm substream
  1030. * @hw: the hardware parameters
  1031. *
  1032. * Sets the substream runtime hardware parameters.
  1033. */
  1034. int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
  1035. const struct snd_pcm_hardware *hw)
  1036. {
  1037. struct snd_pcm_runtime *runtime = substream->runtime;
  1038. runtime->hw.info = hw->info;
  1039. runtime->hw.formats = hw->formats;
  1040. runtime->hw.period_bytes_min = hw->period_bytes_min;
  1041. runtime->hw.period_bytes_max = hw->period_bytes_max;
  1042. runtime->hw.periods_min = hw->periods_min;
  1043. runtime->hw.periods_max = hw->periods_max;
  1044. runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
  1045. runtime->hw.fifo_size = hw->fifo_size;
  1046. return 0;
  1047. }
  1048. EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
  1049. /**
  1050. * snd_soc_cnew - create new control
  1051. * @_template: control template
  1052. * @data: control private data
  1053. * @lnng_name: control long name
  1054. *
  1055. * Create a new mixer control from a template control.
  1056. *
  1057. * Returns 0 for success, else error.
  1058. */
  1059. struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
  1060. void *data, char *long_name)
  1061. {
  1062. struct snd_kcontrol_new template;
  1063. memcpy(&template, _template, sizeof(template));
  1064. if (long_name)
  1065. template.name = long_name;
  1066. template.index = 0;
  1067. return snd_ctl_new1(&template, data);
  1068. }
  1069. EXPORT_SYMBOL_GPL(snd_soc_cnew);
  1070. /**
  1071. * snd_soc_info_enum_double - enumerated double mixer info callback
  1072. * @kcontrol: mixer control
  1073. * @uinfo: control element information
  1074. *
  1075. * Callback to provide information about a double enumerated
  1076. * mixer control.
  1077. *
  1078. * Returns 0 for success.
  1079. */
  1080. int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
  1081. struct snd_ctl_elem_info *uinfo)
  1082. {
  1083. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1084. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  1085. uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
  1086. uinfo->value.enumerated.items = e->mask;
  1087. if (uinfo->value.enumerated.item > e->mask - 1)
  1088. uinfo->value.enumerated.item = e->mask - 1;
  1089. strcpy(uinfo->value.enumerated.name,
  1090. e->texts[uinfo->value.enumerated.item]);
  1091. return 0;
  1092. }
  1093. EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
  1094. /**
  1095. * snd_soc_get_enum_double - enumerated double mixer get callback
  1096. * @kcontrol: mixer control
  1097. * @uinfo: control element information
  1098. *
  1099. * Callback to get the value of a double enumerated mixer.
  1100. *
  1101. * Returns 0 for success.
  1102. */
  1103. int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
  1104. struct snd_ctl_elem_value *ucontrol)
  1105. {
  1106. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1107. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1108. unsigned short val, bitmask;
  1109. for (bitmask = 1; bitmask < e->mask; bitmask <<= 1)
  1110. ;
  1111. val = snd_soc_read(codec, e->reg);
  1112. ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
  1113. if (e->shift_l != e->shift_r)
  1114. ucontrol->value.enumerated.item[1] =
  1115. (val >> e->shift_r) & (bitmask - 1);
  1116. return 0;
  1117. }
  1118. EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
  1119. /**
  1120. * snd_soc_put_enum_double - enumerated double mixer put callback
  1121. * @kcontrol: mixer control
  1122. * @uinfo: control element information
  1123. *
  1124. * Callback to set the value of a double enumerated mixer.
  1125. *
  1126. * Returns 0 for success.
  1127. */
  1128. int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
  1129. struct snd_ctl_elem_value *ucontrol)
  1130. {
  1131. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1132. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1133. unsigned short val;
  1134. unsigned short mask, bitmask;
  1135. for (bitmask = 1; bitmask < e->mask; bitmask <<= 1)
  1136. ;
  1137. if (ucontrol->value.enumerated.item[0] > e->mask - 1)
  1138. return -EINVAL;
  1139. val = ucontrol->value.enumerated.item[0] << e->shift_l;
  1140. mask = (bitmask - 1) << e->shift_l;
  1141. if (e->shift_l != e->shift_r) {
  1142. if (ucontrol->value.enumerated.item[1] > e->mask - 1)
  1143. return -EINVAL;
  1144. val |= ucontrol->value.enumerated.item[1] << e->shift_r;
  1145. mask |= (bitmask - 1) << e->shift_r;
  1146. }
  1147. return snd_soc_update_bits(codec, e->reg, mask, val);
  1148. }
  1149. EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
  1150. /**
  1151. * snd_soc_info_enum_ext - external enumerated single mixer info callback
  1152. * @kcontrol: mixer control
  1153. * @uinfo: control element information
  1154. *
  1155. * Callback to provide information about an external enumerated
  1156. * single mixer.
  1157. *
  1158. * Returns 0 for success.
  1159. */
  1160. int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
  1161. struct snd_ctl_elem_info *uinfo)
  1162. {
  1163. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1164. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  1165. uinfo->count = 1;
  1166. uinfo->value.enumerated.items = e->mask;
  1167. if (uinfo->value.enumerated.item > e->mask - 1)
  1168. uinfo->value.enumerated.item = e->mask - 1;
  1169. strcpy(uinfo->value.enumerated.name,
  1170. e->texts[uinfo->value.enumerated.item]);
  1171. return 0;
  1172. }
  1173. EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
  1174. /**
  1175. * snd_soc_info_volsw_ext - external single mixer info callback
  1176. * @kcontrol: mixer control
  1177. * @uinfo: control element information
  1178. *
  1179. * Callback to provide information about a single external mixer control.
  1180. *
  1181. * Returns 0 for success.
  1182. */
  1183. int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
  1184. struct snd_ctl_elem_info *uinfo)
  1185. {
  1186. int max = kcontrol->private_value;
  1187. if (max == 1)
  1188. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  1189. else
  1190. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1191. uinfo->count = 1;
  1192. uinfo->value.integer.min = 0;
  1193. uinfo->value.integer.max = max;
  1194. return 0;
  1195. }
  1196. EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
  1197. /**
  1198. * snd_soc_info_volsw - single mixer info callback
  1199. * @kcontrol: mixer control
  1200. * @uinfo: control element information
  1201. *
  1202. * Callback to provide information about a single mixer control.
  1203. *
  1204. * Returns 0 for success.
  1205. */
  1206. int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
  1207. struct snd_ctl_elem_info *uinfo)
  1208. {
  1209. int max = (kcontrol->private_value >> 16) & 0xff;
  1210. int shift = (kcontrol->private_value >> 8) & 0x0f;
  1211. int rshift = (kcontrol->private_value >> 12) & 0x0f;
  1212. if (max == 1)
  1213. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  1214. else
  1215. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1216. uinfo->count = shift == rshift ? 1 : 2;
  1217. uinfo->value.integer.min = 0;
  1218. uinfo->value.integer.max = max;
  1219. return 0;
  1220. }
  1221. EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
  1222. /**
  1223. * snd_soc_get_volsw - single mixer get callback
  1224. * @kcontrol: mixer control
  1225. * @uinfo: control element information
  1226. *
  1227. * Callback to get the value of a single mixer control.
  1228. *
  1229. * Returns 0 for success.
  1230. */
  1231. int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
  1232. struct snd_ctl_elem_value *ucontrol)
  1233. {
  1234. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1235. int reg = kcontrol->private_value & 0xff;
  1236. int shift = (kcontrol->private_value >> 8) & 0x0f;
  1237. int rshift = (kcontrol->private_value >> 12) & 0x0f;
  1238. int max = (kcontrol->private_value >> 16) & 0xff;
  1239. int mask = (1 << fls(max)) - 1;
  1240. int invert = (kcontrol->private_value >> 24) & 0x01;
  1241. ucontrol->value.integer.value[0] =
  1242. (snd_soc_read(codec, reg) >> shift) & mask;
  1243. if (shift != rshift)
  1244. ucontrol->value.integer.value[1] =
  1245. (snd_soc_read(codec, reg) >> rshift) & mask;
  1246. if (invert) {
  1247. ucontrol->value.integer.value[0] =
  1248. max - ucontrol->value.integer.value[0];
  1249. if (shift != rshift)
  1250. ucontrol->value.integer.value[1] =
  1251. max - ucontrol->value.integer.value[1];
  1252. }
  1253. return 0;
  1254. }
  1255. EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
  1256. /**
  1257. * snd_soc_put_volsw - single mixer put callback
  1258. * @kcontrol: mixer control
  1259. * @uinfo: control element information
  1260. *
  1261. * Callback to set the value of a single mixer control.
  1262. *
  1263. * Returns 0 for success.
  1264. */
  1265. int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
  1266. struct snd_ctl_elem_value *ucontrol)
  1267. {
  1268. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1269. int reg = kcontrol->private_value & 0xff;
  1270. int shift = (kcontrol->private_value >> 8) & 0x0f;
  1271. int rshift = (kcontrol->private_value >> 12) & 0x0f;
  1272. int max = (kcontrol->private_value >> 16) & 0xff;
  1273. int mask = (1 << fls(max)) - 1;
  1274. int invert = (kcontrol->private_value >> 24) & 0x01;
  1275. unsigned short val, val2, val_mask;
  1276. val = (ucontrol->value.integer.value[0] & mask);
  1277. if (invert)
  1278. val = max - val;
  1279. val_mask = mask << shift;
  1280. val = val << shift;
  1281. if (shift != rshift) {
  1282. val2 = (ucontrol->value.integer.value[1] & mask);
  1283. if (invert)
  1284. val2 = max - val2;
  1285. val_mask |= mask << rshift;
  1286. val |= val2 << rshift;
  1287. }
  1288. return snd_soc_update_bits(codec, reg, val_mask, val);
  1289. }
  1290. EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
  1291. /**
  1292. * snd_soc_info_volsw_2r - double mixer info callback
  1293. * @kcontrol: mixer control
  1294. * @uinfo: control element information
  1295. *
  1296. * Callback to provide information about a double mixer control that
  1297. * spans 2 codec registers.
  1298. *
  1299. * Returns 0 for success.
  1300. */
  1301. int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
  1302. struct snd_ctl_elem_info *uinfo)
  1303. {
  1304. int max = (kcontrol->private_value >> 12) & 0xff;
  1305. if (max == 1)
  1306. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  1307. else
  1308. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1309. uinfo->count = 2;
  1310. uinfo->value.integer.min = 0;
  1311. uinfo->value.integer.max = max;
  1312. return 0;
  1313. }
  1314. EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
  1315. /**
  1316. * snd_soc_get_volsw_2r - double mixer get callback
  1317. * @kcontrol: mixer control
  1318. * @uinfo: control element information
  1319. *
  1320. * Callback to get the value of a double mixer control that spans 2 registers.
  1321. *
  1322. * Returns 0 for success.
  1323. */
  1324. int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
  1325. struct snd_ctl_elem_value *ucontrol)
  1326. {
  1327. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1328. int reg = kcontrol->private_value & 0xff;
  1329. int reg2 = (kcontrol->private_value >> 24) & 0xff;
  1330. int shift = (kcontrol->private_value >> 8) & 0x0f;
  1331. int max = (kcontrol->private_value >> 12) & 0xff;
  1332. int mask = (1<<fls(max))-1;
  1333. int invert = (kcontrol->private_value >> 20) & 0x01;
  1334. ucontrol->value.integer.value[0] =
  1335. (snd_soc_read(codec, reg) >> shift) & mask;
  1336. ucontrol->value.integer.value[1] =
  1337. (snd_soc_read(codec, reg2) >> shift) & mask;
  1338. if (invert) {
  1339. ucontrol->value.integer.value[0] =
  1340. max - ucontrol->value.integer.value[0];
  1341. ucontrol->value.integer.value[1] =
  1342. max - ucontrol->value.integer.value[1];
  1343. }
  1344. return 0;
  1345. }
  1346. EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
  1347. /**
  1348. * snd_soc_put_volsw_2r - double mixer set callback
  1349. * @kcontrol: mixer control
  1350. * @uinfo: control element information
  1351. *
  1352. * Callback to set the value of a double mixer control that spans 2 registers.
  1353. *
  1354. * Returns 0 for success.
  1355. */
  1356. int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
  1357. struct snd_ctl_elem_value *ucontrol)
  1358. {
  1359. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1360. int reg = kcontrol->private_value & 0xff;
  1361. int reg2 = (kcontrol->private_value >> 24) & 0xff;
  1362. int shift = (kcontrol->private_value >> 8) & 0x0f;
  1363. int max = (kcontrol->private_value >> 12) & 0xff;
  1364. int mask = (1 << fls(max)) - 1;
  1365. int invert = (kcontrol->private_value >> 20) & 0x01;
  1366. int err;
  1367. unsigned short val, val2, val_mask;
  1368. val_mask = mask << shift;
  1369. val = (ucontrol->value.integer.value[0] & mask);
  1370. val2 = (ucontrol->value.integer.value[1] & mask);
  1371. if (invert) {
  1372. val = max - val;
  1373. val2 = max - val2;
  1374. }
  1375. val = val << shift;
  1376. val2 = val2 << shift;
  1377. if ((err = snd_soc_update_bits(codec, reg, val_mask, val)) < 0)
  1378. return err;
  1379. err = snd_soc_update_bits(codec, reg2, val_mask, val2);
  1380. return err;
  1381. }
  1382. EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
  1383. static int __devinit snd_soc_init(void)
  1384. {
  1385. printk(KERN_INFO "ASoC version %s\n", SND_SOC_VERSION);
  1386. return platform_driver_register(&soc_driver);
  1387. }
  1388. static void snd_soc_exit(void)
  1389. {
  1390. platform_driver_unregister(&soc_driver);
  1391. }
  1392. module_init(snd_soc_init);
  1393. module_exit(snd_soc_exit);
  1394. /* Module information */
  1395. MODULE_AUTHOR("Liam Girdwood, liam.girdwood@wolfsonmicro.com, www.wolfsonmicro.com");
  1396. MODULE_DESCRIPTION("ALSA SoC Core");
  1397. MODULE_LICENSE("GPL");