soc-core.c 57 KB

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