soc-core.c 51 KB

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