soc-core.c 44 KB

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