soc-core.c 42 KB

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