soc-core.c 42 KB

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