soc-core.c 67 KB

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