soc-core.c 45 KB

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