soc-core.c 67 KB

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