soc-core.c 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556
  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. /**
  683. * snd_soc_suspend_device: Notify core of device suspend
  684. *
  685. * @dev: Device being suspended.
  686. *
  687. * In order to ensure that the entire audio subsystem is suspended in a
  688. * coordinated fashion ASoC devices should suspend themselves when
  689. * called by ASoC. When the standard kernel suspend process asks the
  690. * device to suspend it should call this function to initiate a suspend
  691. * of the entire ASoC card.
  692. *
  693. * \note Currently this function is stubbed out.
  694. */
  695. int snd_soc_suspend_device(struct device *dev)
  696. {
  697. return 0;
  698. }
  699. EXPORT_SYMBOL_GPL(snd_soc_suspend_device);
  700. /**
  701. * snd_soc_resume_device: Notify core of device resume
  702. *
  703. * @dev: Device being resumed.
  704. *
  705. * In order to ensure that the entire audio subsystem is resumed in a
  706. * coordinated fashion ASoC devices should resume themselves when called
  707. * by ASoC. When the standard kernel resume process asks the device
  708. * to resume it should call this function. Once all the components of
  709. * the card have notified that they are ready to be resumed the card
  710. * will be resumed.
  711. *
  712. * \note Currently this function is stubbed out.
  713. */
  714. int snd_soc_resume_device(struct device *dev)
  715. {
  716. return 0;
  717. }
  718. EXPORT_SYMBOL_GPL(snd_soc_resume_device);
  719. #else
  720. #define soc_suspend NULL
  721. #define soc_resume NULL
  722. #endif
  723. static void snd_soc_instantiate_card(struct snd_soc_card *card)
  724. {
  725. struct platform_device *pdev = container_of(card->dev,
  726. struct platform_device,
  727. dev);
  728. struct snd_soc_codec_device *codec_dev = card->socdev->codec_dev;
  729. struct snd_soc_platform *platform;
  730. struct snd_soc_dai *dai;
  731. int i, found, ret, ac97;
  732. if (card->instantiated)
  733. return;
  734. found = 0;
  735. list_for_each_entry(platform, &platform_list, list)
  736. if (card->platform == platform) {
  737. found = 1;
  738. break;
  739. }
  740. if (!found) {
  741. dev_dbg(card->dev, "Platform %s not registered\n",
  742. card->platform->name);
  743. return;
  744. }
  745. ac97 = 0;
  746. for (i = 0; i < card->num_links; i++) {
  747. found = 0;
  748. list_for_each_entry(dai, &dai_list, list)
  749. if (card->dai_link[i].cpu_dai == dai) {
  750. found = 1;
  751. break;
  752. }
  753. if (!found) {
  754. dev_dbg(card->dev, "DAI %s not registered\n",
  755. card->dai_link[i].cpu_dai->name);
  756. return;
  757. }
  758. if (card->dai_link[i].cpu_dai->ac97_control)
  759. ac97 = 1;
  760. }
  761. /* If we have AC97 in the system then don't wait for the
  762. * codec. This will need revisiting if we have to handle
  763. * systems with mixed AC97 and non-AC97 parts. Only check for
  764. * DAIs currently; we can't do this per link since some AC97
  765. * codecs have non-AC97 DAIs.
  766. */
  767. if (!ac97)
  768. for (i = 0; i < card->num_links; i++) {
  769. found = 0;
  770. list_for_each_entry(dai, &dai_list, list)
  771. if (card->dai_link[i].codec_dai == dai) {
  772. found = 1;
  773. break;
  774. }
  775. if (!found) {
  776. dev_dbg(card->dev, "DAI %s not registered\n",
  777. card->dai_link[i].codec_dai->name);
  778. return;
  779. }
  780. }
  781. /* Note that we do not current check for codec components */
  782. dev_dbg(card->dev, "All components present, instantiating\n");
  783. /* Found everything, bring it up */
  784. if (card->probe) {
  785. ret = card->probe(pdev);
  786. if (ret < 0)
  787. return;
  788. }
  789. for (i = 0; i < card->num_links; i++) {
  790. struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
  791. if (cpu_dai->probe) {
  792. ret = cpu_dai->probe(pdev, cpu_dai);
  793. if (ret < 0)
  794. goto cpu_dai_err;
  795. }
  796. }
  797. if (codec_dev->probe) {
  798. ret = codec_dev->probe(pdev);
  799. if (ret < 0)
  800. goto cpu_dai_err;
  801. }
  802. if (platform->probe) {
  803. ret = platform->probe(pdev);
  804. if (ret < 0)
  805. goto platform_err;
  806. }
  807. /* DAPM stream work */
  808. INIT_DELAYED_WORK(&card->delayed_work, close_delayed_work);
  809. #ifdef CONFIG_PM
  810. /* deferred resume work */
  811. INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
  812. #endif
  813. card->instantiated = 1;
  814. return;
  815. platform_err:
  816. if (codec_dev->remove)
  817. codec_dev->remove(pdev);
  818. cpu_dai_err:
  819. for (i--; i >= 0; i--) {
  820. struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
  821. if (cpu_dai->remove)
  822. cpu_dai->remove(pdev, cpu_dai);
  823. }
  824. if (card->remove)
  825. card->remove(pdev);
  826. }
  827. /*
  828. * Attempt to initialise any uninitalised cards. Must be called with
  829. * client_mutex.
  830. */
  831. static void snd_soc_instantiate_cards(void)
  832. {
  833. struct snd_soc_card *card;
  834. list_for_each_entry(card, &card_list, list)
  835. snd_soc_instantiate_card(card);
  836. }
  837. /* probes a new socdev */
  838. static int soc_probe(struct platform_device *pdev)
  839. {
  840. int ret = 0;
  841. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  842. struct snd_soc_card *card = socdev->card;
  843. /* Bodge while we push things out of socdev */
  844. card->socdev = socdev;
  845. /* Bodge while we unpick instantiation */
  846. card->dev = &pdev->dev;
  847. ret = snd_soc_register_card(card);
  848. if (ret != 0) {
  849. dev_err(&pdev->dev, "Failed to register card\n");
  850. return ret;
  851. }
  852. return 0;
  853. }
  854. /* removes a socdev */
  855. static int soc_remove(struct platform_device *pdev)
  856. {
  857. int i;
  858. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  859. struct snd_soc_card *card = socdev->card;
  860. struct snd_soc_platform *platform = card->platform;
  861. struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
  862. if (!card->instantiated)
  863. return 0;
  864. run_delayed_work(&card->delayed_work);
  865. if (platform->remove)
  866. platform->remove(pdev);
  867. if (codec_dev->remove)
  868. codec_dev->remove(pdev);
  869. for (i = 0; i < card->num_links; i++) {
  870. struct snd_soc_dai *cpu_dai = card->dai_link[i].cpu_dai;
  871. if (cpu_dai->remove)
  872. cpu_dai->remove(pdev, cpu_dai);
  873. }
  874. if (card->remove)
  875. card->remove(pdev);
  876. snd_soc_unregister_card(card);
  877. return 0;
  878. }
  879. static int soc_poweroff(struct device *dev)
  880. {
  881. struct platform_device *pdev = to_platform_device(dev);
  882. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  883. struct snd_soc_card *card = socdev->card;
  884. if (!card->instantiated)
  885. return 0;
  886. /* Flush out pmdown_time work - we actually do want to run it
  887. * now, we're shutting down so no imminent restart. */
  888. run_delayed_work(&card->delayed_work);
  889. snd_soc_dapm_shutdown(socdev);
  890. return 0;
  891. }
  892. static struct dev_pm_ops soc_pm_ops = {
  893. .suspend = soc_suspend,
  894. .resume = soc_resume,
  895. .poweroff = soc_poweroff,
  896. };
  897. /* ASoC platform driver */
  898. static struct platform_driver soc_driver = {
  899. .driver = {
  900. .name = "soc-audio",
  901. .owner = THIS_MODULE,
  902. .pm = &soc_pm_ops,
  903. },
  904. .probe = soc_probe,
  905. .remove = soc_remove,
  906. };
  907. /* create a new pcm */
  908. static int soc_new_pcm(struct snd_soc_device *socdev,
  909. struct snd_soc_dai_link *dai_link, int num)
  910. {
  911. struct snd_soc_card *card = socdev->card;
  912. struct snd_soc_codec *codec = card->codec;
  913. struct snd_soc_platform *platform = card->platform;
  914. struct snd_soc_dai *codec_dai = dai_link->codec_dai;
  915. struct snd_soc_dai *cpu_dai = dai_link->cpu_dai;
  916. struct snd_soc_pcm_runtime *rtd;
  917. struct snd_pcm *pcm;
  918. char new_name[64];
  919. int ret = 0, playback = 0, capture = 0;
  920. rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
  921. if (rtd == NULL)
  922. return -ENOMEM;
  923. rtd->dai = dai_link;
  924. rtd->socdev = socdev;
  925. codec_dai->codec = card->codec;
  926. /* check client and interface hw capabilities */
  927. sprintf(new_name, "%s %s-%d", dai_link->stream_name, codec_dai->name,
  928. num);
  929. if (codec_dai->playback.channels_min)
  930. playback = 1;
  931. if (codec_dai->capture.channels_min)
  932. capture = 1;
  933. ret = snd_pcm_new(codec->card, new_name, codec->pcm_devs++, playback,
  934. capture, &pcm);
  935. if (ret < 0) {
  936. printk(KERN_ERR "asoc: can't create pcm for codec %s\n",
  937. codec->name);
  938. kfree(rtd);
  939. return ret;
  940. }
  941. dai_link->pcm = pcm;
  942. pcm->private_data = rtd;
  943. soc_pcm_ops.mmap = platform->pcm_ops->mmap;
  944. soc_pcm_ops.pointer = platform->pcm_ops->pointer;
  945. soc_pcm_ops.ioctl = platform->pcm_ops->ioctl;
  946. soc_pcm_ops.copy = platform->pcm_ops->copy;
  947. soc_pcm_ops.silence = platform->pcm_ops->silence;
  948. soc_pcm_ops.ack = platform->pcm_ops->ack;
  949. soc_pcm_ops.page = platform->pcm_ops->page;
  950. if (playback)
  951. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
  952. if (capture)
  953. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
  954. ret = platform->pcm_new(codec->card, codec_dai, pcm);
  955. if (ret < 0) {
  956. printk(KERN_ERR "asoc: platform pcm constructor failed\n");
  957. kfree(rtd);
  958. return ret;
  959. }
  960. pcm->private_free = platform->pcm_free;
  961. printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
  962. cpu_dai->name);
  963. return ret;
  964. }
  965. /**
  966. * snd_soc_codec_volatile_register: Report if a register is volatile.
  967. *
  968. * @codec: CODEC to query.
  969. * @reg: Register to query.
  970. *
  971. * Boolean function indiciating if a CODEC register is volatile.
  972. */
  973. int snd_soc_codec_volatile_register(struct snd_soc_codec *codec, int reg)
  974. {
  975. if (codec->volatile_register)
  976. return codec->volatile_register(reg);
  977. else
  978. return 0;
  979. }
  980. EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
  981. /* codec register dump */
  982. static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf)
  983. {
  984. int i, step = 1, count = 0;
  985. if (!codec->reg_cache_size)
  986. return 0;
  987. if (codec->reg_cache_step)
  988. step = codec->reg_cache_step;
  989. count += sprintf(buf, "%s registers\n", codec->name);
  990. for (i = 0; i < codec->reg_cache_size; i += step) {
  991. count += sprintf(buf + count, "%2x: ", i);
  992. if (count >= PAGE_SIZE - 1)
  993. break;
  994. if (codec->display_register)
  995. count += codec->display_register(codec, buf + count,
  996. PAGE_SIZE - count, i);
  997. else
  998. count += snprintf(buf + count, PAGE_SIZE - count,
  999. "%4x", codec->read(codec, i));
  1000. if (count >= PAGE_SIZE - 1)
  1001. break;
  1002. count += snprintf(buf + count, PAGE_SIZE - count, "\n");
  1003. if (count >= PAGE_SIZE - 1)
  1004. break;
  1005. }
  1006. /* Truncate count; min() would cause a warning */
  1007. if (count >= PAGE_SIZE)
  1008. count = PAGE_SIZE - 1;
  1009. return count;
  1010. }
  1011. static ssize_t codec_reg_show(struct device *dev,
  1012. struct device_attribute *attr, char *buf)
  1013. {
  1014. struct snd_soc_device *devdata = dev_get_drvdata(dev);
  1015. return soc_codec_reg_show(devdata->card->codec, buf);
  1016. }
  1017. static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
  1018. #ifdef CONFIG_DEBUG_FS
  1019. static int codec_reg_open_file(struct inode *inode, struct file *file)
  1020. {
  1021. file->private_data = inode->i_private;
  1022. return 0;
  1023. }
  1024. static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
  1025. size_t count, loff_t *ppos)
  1026. {
  1027. ssize_t ret;
  1028. struct snd_soc_codec *codec = file->private_data;
  1029. char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  1030. if (!buf)
  1031. return -ENOMEM;
  1032. ret = soc_codec_reg_show(codec, buf);
  1033. if (ret >= 0)
  1034. ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
  1035. kfree(buf);
  1036. return ret;
  1037. }
  1038. static ssize_t codec_reg_write_file(struct file *file,
  1039. const char __user *user_buf, size_t count, loff_t *ppos)
  1040. {
  1041. char buf[32];
  1042. int buf_size;
  1043. char *start = buf;
  1044. unsigned long reg, value;
  1045. int step = 1;
  1046. struct snd_soc_codec *codec = file->private_data;
  1047. buf_size = min(count, (sizeof(buf)-1));
  1048. if (copy_from_user(buf, user_buf, buf_size))
  1049. return -EFAULT;
  1050. buf[buf_size] = 0;
  1051. if (codec->reg_cache_step)
  1052. step = codec->reg_cache_step;
  1053. while (*start == ' ')
  1054. start++;
  1055. reg = simple_strtoul(start, &start, 16);
  1056. if ((reg >= codec->reg_cache_size) || (reg % step))
  1057. return -EINVAL;
  1058. while (*start == ' ')
  1059. start++;
  1060. if (strict_strtoul(start, 16, &value))
  1061. return -EINVAL;
  1062. codec->write(codec, reg, value);
  1063. return buf_size;
  1064. }
  1065. static const struct file_operations codec_reg_fops = {
  1066. .open = codec_reg_open_file,
  1067. .read = codec_reg_read_file,
  1068. .write = codec_reg_write_file,
  1069. };
  1070. static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
  1071. {
  1072. codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
  1073. debugfs_root, codec,
  1074. &codec_reg_fops);
  1075. if (!codec->debugfs_reg)
  1076. printk(KERN_WARNING
  1077. "ASoC: Failed to create codec register debugfs file\n");
  1078. codec->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0744,
  1079. debugfs_root,
  1080. &codec->pop_time);
  1081. if (!codec->debugfs_pop_time)
  1082. printk(KERN_WARNING
  1083. "Failed to create pop time debugfs file\n");
  1084. }
  1085. static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
  1086. {
  1087. debugfs_remove(codec->debugfs_pop_time);
  1088. debugfs_remove(codec->debugfs_reg);
  1089. }
  1090. #else
  1091. static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
  1092. {
  1093. }
  1094. static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
  1095. {
  1096. }
  1097. #endif
  1098. /**
  1099. * snd_soc_new_ac97_codec - initailise AC97 device
  1100. * @codec: audio codec
  1101. * @ops: AC97 bus operations
  1102. * @num: AC97 codec number
  1103. *
  1104. * Initialises AC97 codec resources for use by ad-hoc devices only.
  1105. */
  1106. int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
  1107. struct snd_ac97_bus_ops *ops, int num)
  1108. {
  1109. mutex_lock(&codec->mutex);
  1110. codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
  1111. if (codec->ac97 == NULL) {
  1112. mutex_unlock(&codec->mutex);
  1113. return -ENOMEM;
  1114. }
  1115. codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
  1116. if (codec->ac97->bus == NULL) {
  1117. kfree(codec->ac97);
  1118. codec->ac97 = NULL;
  1119. mutex_unlock(&codec->mutex);
  1120. return -ENOMEM;
  1121. }
  1122. codec->ac97->bus->ops = ops;
  1123. codec->ac97->num = num;
  1124. mutex_unlock(&codec->mutex);
  1125. return 0;
  1126. }
  1127. EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
  1128. /**
  1129. * snd_soc_free_ac97_codec - free AC97 codec device
  1130. * @codec: audio codec
  1131. *
  1132. * Frees AC97 codec device resources.
  1133. */
  1134. void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
  1135. {
  1136. mutex_lock(&codec->mutex);
  1137. kfree(codec->ac97->bus);
  1138. kfree(codec->ac97);
  1139. codec->ac97 = NULL;
  1140. mutex_unlock(&codec->mutex);
  1141. }
  1142. EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
  1143. /**
  1144. * snd_soc_update_bits - update codec register bits
  1145. * @codec: audio codec
  1146. * @reg: codec register
  1147. * @mask: register mask
  1148. * @value: new value
  1149. *
  1150. * Writes new register value.
  1151. *
  1152. * Returns 1 for change else 0.
  1153. */
  1154. int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
  1155. unsigned int mask, unsigned int value)
  1156. {
  1157. int change;
  1158. unsigned int old, new;
  1159. mutex_lock(&io_mutex);
  1160. old = snd_soc_read(codec, reg);
  1161. new = (old & ~mask) | value;
  1162. change = old != new;
  1163. if (change)
  1164. snd_soc_write(codec, reg, new);
  1165. mutex_unlock(&io_mutex);
  1166. return change;
  1167. }
  1168. EXPORT_SYMBOL_GPL(snd_soc_update_bits);
  1169. /**
  1170. * snd_soc_test_bits - test register for change
  1171. * @codec: audio codec
  1172. * @reg: codec register
  1173. * @mask: register mask
  1174. * @value: new value
  1175. *
  1176. * Tests a register with a new value and checks if the new value is
  1177. * different from the old value.
  1178. *
  1179. * Returns 1 for change else 0.
  1180. */
  1181. int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
  1182. unsigned int mask, unsigned int value)
  1183. {
  1184. int change;
  1185. unsigned int old, new;
  1186. mutex_lock(&io_mutex);
  1187. old = snd_soc_read(codec, reg);
  1188. new = (old & ~mask) | value;
  1189. change = old != new;
  1190. mutex_unlock(&io_mutex);
  1191. return change;
  1192. }
  1193. EXPORT_SYMBOL_GPL(snd_soc_test_bits);
  1194. /**
  1195. * snd_soc_new_pcms - create new sound card and pcms
  1196. * @socdev: the SoC audio device
  1197. * @idx: ALSA card index
  1198. * @xid: card identification
  1199. *
  1200. * Create a new sound card based upon the codec and interface pcms.
  1201. *
  1202. * Returns 0 for success, else error.
  1203. */
  1204. int snd_soc_new_pcms(struct snd_soc_device *socdev, int idx, const char *xid)
  1205. {
  1206. struct snd_soc_card *card = socdev->card;
  1207. struct snd_soc_codec *codec = card->codec;
  1208. int ret, i;
  1209. mutex_lock(&codec->mutex);
  1210. /* register a sound card */
  1211. ret = snd_card_create(idx, xid, codec->owner, 0, &codec->card);
  1212. if (ret < 0) {
  1213. printk(KERN_ERR "asoc: can't create sound card for codec %s\n",
  1214. codec->name);
  1215. mutex_unlock(&codec->mutex);
  1216. return ret;
  1217. }
  1218. codec->socdev = socdev;
  1219. codec->card->dev = socdev->dev;
  1220. codec->card->private_data = codec;
  1221. strncpy(codec->card->driver, codec->name, sizeof(codec->card->driver));
  1222. /* create the pcms */
  1223. for (i = 0; i < card->num_links; i++) {
  1224. ret = soc_new_pcm(socdev, &card->dai_link[i], i);
  1225. if (ret < 0) {
  1226. printk(KERN_ERR "asoc: can't create pcm %s\n",
  1227. card->dai_link[i].stream_name);
  1228. mutex_unlock(&codec->mutex);
  1229. return ret;
  1230. }
  1231. }
  1232. mutex_unlock(&codec->mutex);
  1233. return ret;
  1234. }
  1235. EXPORT_SYMBOL_GPL(snd_soc_new_pcms);
  1236. /**
  1237. * snd_soc_init_card - register sound card
  1238. * @socdev: the SoC audio device
  1239. *
  1240. * Register a SoC sound card. Also registers an AC97 device if the
  1241. * codec is AC97 for ad hoc devices.
  1242. *
  1243. * Returns 0 for success, else error.
  1244. */
  1245. int snd_soc_init_card(struct snd_soc_device *socdev)
  1246. {
  1247. struct snd_soc_card *card = socdev->card;
  1248. struct snd_soc_codec *codec = card->codec;
  1249. int ret = 0, i, ac97 = 0, err = 0;
  1250. for (i = 0; i < card->num_links; i++) {
  1251. if (card->dai_link[i].init) {
  1252. err = card->dai_link[i].init(codec);
  1253. if (err < 0) {
  1254. printk(KERN_ERR "asoc: failed to init %s\n",
  1255. card->dai_link[i].stream_name);
  1256. continue;
  1257. }
  1258. }
  1259. if (card->dai_link[i].codec_dai->ac97_control) {
  1260. ac97 = 1;
  1261. snd_ac97_dev_add_pdata(codec->ac97,
  1262. card->dai_link[i].cpu_dai->ac97_pdata);
  1263. }
  1264. }
  1265. snprintf(codec->card->shortname, sizeof(codec->card->shortname),
  1266. "%s", card->name);
  1267. snprintf(codec->card->longname, sizeof(codec->card->longname),
  1268. "%s (%s)", card->name, codec->name);
  1269. /* Make sure all DAPM widgets are instantiated */
  1270. snd_soc_dapm_new_widgets(codec);
  1271. ret = snd_card_register(codec->card);
  1272. if (ret < 0) {
  1273. printk(KERN_ERR "asoc: failed to register soundcard for %s\n",
  1274. codec->name);
  1275. goto out;
  1276. }
  1277. mutex_lock(&codec->mutex);
  1278. #ifdef CONFIG_SND_SOC_AC97_BUS
  1279. /* Only instantiate AC97 if not already done by the adaptor
  1280. * for the generic AC97 subsystem.
  1281. */
  1282. if (ac97 && strcmp(codec->name, "AC97") != 0) {
  1283. ret = soc_ac97_dev_register(codec);
  1284. if (ret < 0) {
  1285. printk(KERN_ERR "asoc: AC97 device register failed\n");
  1286. snd_card_free(codec->card);
  1287. mutex_unlock(&codec->mutex);
  1288. goto out;
  1289. }
  1290. }
  1291. #endif
  1292. err = snd_soc_dapm_sys_add(socdev->dev);
  1293. if (err < 0)
  1294. printk(KERN_WARNING "asoc: failed to add dapm sysfs entries\n");
  1295. err = device_create_file(socdev->dev, &dev_attr_codec_reg);
  1296. if (err < 0)
  1297. printk(KERN_WARNING "asoc: failed to add codec sysfs files\n");
  1298. soc_init_codec_debugfs(codec);
  1299. mutex_unlock(&codec->mutex);
  1300. out:
  1301. return ret;
  1302. }
  1303. EXPORT_SYMBOL_GPL(snd_soc_init_card);
  1304. /**
  1305. * snd_soc_free_pcms - free sound card and pcms
  1306. * @socdev: the SoC audio device
  1307. *
  1308. * Frees sound card and pcms associated with the socdev.
  1309. * Also unregister the codec if it is an AC97 device.
  1310. */
  1311. void snd_soc_free_pcms(struct snd_soc_device *socdev)
  1312. {
  1313. struct snd_soc_codec *codec = socdev->card->codec;
  1314. #ifdef CONFIG_SND_SOC_AC97_BUS
  1315. struct snd_soc_dai *codec_dai;
  1316. int i;
  1317. #endif
  1318. mutex_lock(&codec->mutex);
  1319. soc_cleanup_codec_debugfs(codec);
  1320. #ifdef CONFIG_SND_SOC_AC97_BUS
  1321. for (i = 0; i < codec->num_dai; i++) {
  1322. codec_dai = &codec->dai[i];
  1323. if (codec_dai->ac97_control && codec->ac97 &&
  1324. strcmp(codec->name, "AC97") != 0) {
  1325. soc_ac97_dev_unregister(codec);
  1326. goto free_card;
  1327. }
  1328. }
  1329. free_card:
  1330. #endif
  1331. if (codec->card)
  1332. snd_card_free(codec->card);
  1333. device_remove_file(socdev->dev, &dev_attr_codec_reg);
  1334. mutex_unlock(&codec->mutex);
  1335. }
  1336. EXPORT_SYMBOL_GPL(snd_soc_free_pcms);
  1337. /**
  1338. * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
  1339. * @substream: the pcm substream
  1340. * @hw: the hardware parameters
  1341. *
  1342. * Sets the substream runtime hardware parameters.
  1343. */
  1344. int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
  1345. const struct snd_pcm_hardware *hw)
  1346. {
  1347. struct snd_pcm_runtime *runtime = substream->runtime;
  1348. runtime->hw.info = hw->info;
  1349. runtime->hw.formats = hw->formats;
  1350. runtime->hw.period_bytes_min = hw->period_bytes_min;
  1351. runtime->hw.period_bytes_max = hw->period_bytes_max;
  1352. runtime->hw.periods_min = hw->periods_min;
  1353. runtime->hw.periods_max = hw->periods_max;
  1354. runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
  1355. runtime->hw.fifo_size = hw->fifo_size;
  1356. return 0;
  1357. }
  1358. EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
  1359. /**
  1360. * snd_soc_cnew - create new control
  1361. * @_template: control template
  1362. * @data: control private data
  1363. * @long_name: control long name
  1364. *
  1365. * Create a new mixer control from a template control.
  1366. *
  1367. * Returns 0 for success, else error.
  1368. */
  1369. struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
  1370. void *data, char *long_name)
  1371. {
  1372. struct snd_kcontrol_new template;
  1373. memcpy(&template, _template, sizeof(template));
  1374. if (long_name)
  1375. template.name = long_name;
  1376. template.index = 0;
  1377. return snd_ctl_new1(&template, data);
  1378. }
  1379. EXPORT_SYMBOL_GPL(snd_soc_cnew);
  1380. /**
  1381. * snd_soc_add_controls - add an array of controls to a codec.
  1382. * Convienience function to add a list of controls. Many codecs were
  1383. * duplicating this code.
  1384. *
  1385. * @codec: codec to add controls to
  1386. * @controls: array of controls to add
  1387. * @num_controls: number of elements in the array
  1388. *
  1389. * Return 0 for success, else error.
  1390. */
  1391. int snd_soc_add_controls(struct snd_soc_codec *codec,
  1392. const struct snd_kcontrol_new *controls, int num_controls)
  1393. {
  1394. struct snd_card *card = codec->card;
  1395. int err, i;
  1396. for (i = 0; i < num_controls; i++) {
  1397. const struct snd_kcontrol_new *control = &controls[i];
  1398. err = snd_ctl_add(card, snd_soc_cnew(control, codec, NULL));
  1399. if (err < 0) {
  1400. dev_err(codec->dev, "%s: Failed to add %s\n",
  1401. codec->name, control->name);
  1402. return err;
  1403. }
  1404. }
  1405. return 0;
  1406. }
  1407. EXPORT_SYMBOL_GPL(snd_soc_add_controls);
  1408. /**
  1409. * snd_soc_info_enum_double - enumerated double mixer info callback
  1410. * @kcontrol: mixer control
  1411. * @uinfo: control element information
  1412. *
  1413. * Callback to provide information about a double enumerated
  1414. * mixer control.
  1415. *
  1416. * Returns 0 for success.
  1417. */
  1418. int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
  1419. struct snd_ctl_elem_info *uinfo)
  1420. {
  1421. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1422. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  1423. uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
  1424. uinfo->value.enumerated.items = e->max;
  1425. if (uinfo->value.enumerated.item > e->max - 1)
  1426. uinfo->value.enumerated.item = e->max - 1;
  1427. strcpy(uinfo->value.enumerated.name,
  1428. e->texts[uinfo->value.enumerated.item]);
  1429. return 0;
  1430. }
  1431. EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
  1432. /**
  1433. * snd_soc_get_enum_double - enumerated double mixer get callback
  1434. * @kcontrol: mixer control
  1435. * @ucontrol: control element information
  1436. *
  1437. * Callback to get the value of a double enumerated mixer.
  1438. *
  1439. * Returns 0 for success.
  1440. */
  1441. int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
  1442. struct snd_ctl_elem_value *ucontrol)
  1443. {
  1444. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1445. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1446. unsigned int val, bitmask;
  1447. for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
  1448. ;
  1449. val = snd_soc_read(codec, e->reg);
  1450. ucontrol->value.enumerated.item[0]
  1451. = (val >> e->shift_l) & (bitmask - 1);
  1452. if (e->shift_l != e->shift_r)
  1453. ucontrol->value.enumerated.item[1] =
  1454. (val >> e->shift_r) & (bitmask - 1);
  1455. return 0;
  1456. }
  1457. EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
  1458. /**
  1459. * snd_soc_put_enum_double - enumerated double mixer put callback
  1460. * @kcontrol: mixer control
  1461. * @ucontrol: control element information
  1462. *
  1463. * Callback to set the value of a double enumerated mixer.
  1464. *
  1465. * Returns 0 for success.
  1466. */
  1467. int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
  1468. struct snd_ctl_elem_value *ucontrol)
  1469. {
  1470. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1471. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1472. unsigned int val;
  1473. unsigned int mask, bitmask;
  1474. for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
  1475. ;
  1476. if (ucontrol->value.enumerated.item[0] > e->max - 1)
  1477. return -EINVAL;
  1478. val = ucontrol->value.enumerated.item[0] << e->shift_l;
  1479. mask = (bitmask - 1) << e->shift_l;
  1480. if (e->shift_l != e->shift_r) {
  1481. if (ucontrol->value.enumerated.item[1] > e->max - 1)
  1482. return -EINVAL;
  1483. val |= ucontrol->value.enumerated.item[1] << e->shift_r;
  1484. mask |= (bitmask - 1) << e->shift_r;
  1485. }
  1486. return snd_soc_update_bits(codec, e->reg, mask, val);
  1487. }
  1488. EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
  1489. /**
  1490. * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
  1491. * @kcontrol: mixer control
  1492. * @ucontrol: control element information
  1493. *
  1494. * Callback to get the value of a double semi enumerated mixer.
  1495. *
  1496. * Semi enumerated mixer: the enumerated items are referred as values. Can be
  1497. * used for handling bitfield coded enumeration for example.
  1498. *
  1499. * Returns 0 for success.
  1500. */
  1501. int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
  1502. struct snd_ctl_elem_value *ucontrol)
  1503. {
  1504. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1505. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1506. unsigned int reg_val, val, mux;
  1507. reg_val = snd_soc_read(codec, e->reg);
  1508. val = (reg_val >> e->shift_l) & e->mask;
  1509. for (mux = 0; mux < e->max; mux++) {
  1510. if (val == e->values[mux])
  1511. break;
  1512. }
  1513. ucontrol->value.enumerated.item[0] = mux;
  1514. if (e->shift_l != e->shift_r) {
  1515. val = (reg_val >> e->shift_r) & e->mask;
  1516. for (mux = 0; mux < e->max; mux++) {
  1517. if (val == e->values[mux])
  1518. break;
  1519. }
  1520. ucontrol->value.enumerated.item[1] = mux;
  1521. }
  1522. return 0;
  1523. }
  1524. EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
  1525. /**
  1526. * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
  1527. * @kcontrol: mixer control
  1528. * @ucontrol: control element information
  1529. *
  1530. * Callback to set the value of a double semi enumerated mixer.
  1531. *
  1532. * Semi enumerated mixer: the enumerated items are referred as values. Can be
  1533. * used for handling bitfield coded enumeration for example.
  1534. *
  1535. * Returns 0 for success.
  1536. */
  1537. int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
  1538. struct snd_ctl_elem_value *ucontrol)
  1539. {
  1540. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1541. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1542. unsigned int val;
  1543. unsigned int mask;
  1544. if (ucontrol->value.enumerated.item[0] > e->max - 1)
  1545. return -EINVAL;
  1546. val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
  1547. mask = e->mask << e->shift_l;
  1548. if (e->shift_l != e->shift_r) {
  1549. if (ucontrol->value.enumerated.item[1] > e->max - 1)
  1550. return -EINVAL;
  1551. val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
  1552. mask |= e->mask << e->shift_r;
  1553. }
  1554. return snd_soc_update_bits(codec, e->reg, mask, val);
  1555. }
  1556. EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
  1557. /**
  1558. * snd_soc_info_enum_ext - external enumerated single mixer info callback
  1559. * @kcontrol: mixer control
  1560. * @uinfo: control element information
  1561. *
  1562. * Callback to provide information about an external enumerated
  1563. * single mixer.
  1564. *
  1565. * Returns 0 for success.
  1566. */
  1567. int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
  1568. struct snd_ctl_elem_info *uinfo)
  1569. {
  1570. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1571. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  1572. uinfo->count = 1;
  1573. uinfo->value.enumerated.items = e->max;
  1574. if (uinfo->value.enumerated.item > e->max - 1)
  1575. uinfo->value.enumerated.item = e->max - 1;
  1576. strcpy(uinfo->value.enumerated.name,
  1577. e->texts[uinfo->value.enumerated.item]);
  1578. return 0;
  1579. }
  1580. EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
  1581. /**
  1582. * snd_soc_info_volsw_ext - external single mixer info callback
  1583. * @kcontrol: mixer control
  1584. * @uinfo: control element information
  1585. *
  1586. * Callback to provide information about a single external mixer control.
  1587. *
  1588. * Returns 0 for success.
  1589. */
  1590. int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
  1591. struct snd_ctl_elem_info *uinfo)
  1592. {
  1593. int max = kcontrol->private_value;
  1594. if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
  1595. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  1596. else
  1597. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1598. uinfo->count = 1;
  1599. uinfo->value.integer.min = 0;
  1600. uinfo->value.integer.max = max;
  1601. return 0;
  1602. }
  1603. EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
  1604. /**
  1605. * snd_soc_info_volsw - single mixer info callback
  1606. * @kcontrol: mixer control
  1607. * @uinfo: control element information
  1608. *
  1609. * Callback to provide information about a single mixer control.
  1610. *
  1611. * Returns 0 for success.
  1612. */
  1613. int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
  1614. struct snd_ctl_elem_info *uinfo)
  1615. {
  1616. struct soc_mixer_control *mc =
  1617. (struct soc_mixer_control *)kcontrol->private_value;
  1618. int max = mc->max;
  1619. unsigned int shift = mc->shift;
  1620. unsigned int rshift = mc->rshift;
  1621. if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
  1622. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  1623. else
  1624. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1625. uinfo->count = shift == rshift ? 1 : 2;
  1626. uinfo->value.integer.min = 0;
  1627. uinfo->value.integer.max = max;
  1628. return 0;
  1629. }
  1630. EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
  1631. /**
  1632. * snd_soc_get_volsw - single mixer get callback
  1633. * @kcontrol: mixer control
  1634. * @ucontrol: control element information
  1635. *
  1636. * Callback to get the value of a single mixer control.
  1637. *
  1638. * Returns 0 for success.
  1639. */
  1640. int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
  1641. struct snd_ctl_elem_value *ucontrol)
  1642. {
  1643. struct soc_mixer_control *mc =
  1644. (struct soc_mixer_control *)kcontrol->private_value;
  1645. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1646. unsigned int reg = mc->reg;
  1647. unsigned int shift = mc->shift;
  1648. unsigned int rshift = mc->rshift;
  1649. int max = mc->max;
  1650. unsigned int mask = (1 << fls(max)) - 1;
  1651. unsigned int invert = mc->invert;
  1652. ucontrol->value.integer.value[0] =
  1653. (snd_soc_read(codec, reg) >> shift) & mask;
  1654. if (shift != rshift)
  1655. ucontrol->value.integer.value[1] =
  1656. (snd_soc_read(codec, reg) >> rshift) & mask;
  1657. if (invert) {
  1658. ucontrol->value.integer.value[0] =
  1659. max - ucontrol->value.integer.value[0];
  1660. if (shift != rshift)
  1661. ucontrol->value.integer.value[1] =
  1662. max - ucontrol->value.integer.value[1];
  1663. }
  1664. return 0;
  1665. }
  1666. EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
  1667. /**
  1668. * snd_soc_put_volsw - single mixer put callback
  1669. * @kcontrol: mixer control
  1670. * @ucontrol: control element information
  1671. *
  1672. * Callback to set the value of a single mixer control.
  1673. *
  1674. * Returns 0 for success.
  1675. */
  1676. int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
  1677. struct snd_ctl_elem_value *ucontrol)
  1678. {
  1679. struct soc_mixer_control *mc =
  1680. (struct soc_mixer_control *)kcontrol->private_value;
  1681. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1682. unsigned int reg = mc->reg;
  1683. unsigned int shift = mc->shift;
  1684. unsigned int rshift = mc->rshift;
  1685. int max = mc->max;
  1686. unsigned int mask = (1 << fls(max)) - 1;
  1687. unsigned int invert = mc->invert;
  1688. unsigned int val, val2, val_mask;
  1689. val = (ucontrol->value.integer.value[0] & mask);
  1690. if (invert)
  1691. val = max - val;
  1692. val_mask = mask << shift;
  1693. val = val << shift;
  1694. if (shift != rshift) {
  1695. val2 = (ucontrol->value.integer.value[1] & mask);
  1696. if (invert)
  1697. val2 = max - val2;
  1698. val_mask |= mask << rshift;
  1699. val |= val2 << rshift;
  1700. }
  1701. return snd_soc_update_bits(codec, reg, val_mask, val);
  1702. }
  1703. EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
  1704. /**
  1705. * snd_soc_info_volsw_2r - double mixer info callback
  1706. * @kcontrol: mixer control
  1707. * @uinfo: control element information
  1708. *
  1709. * Callback to provide information about a double mixer control that
  1710. * spans 2 codec registers.
  1711. *
  1712. * Returns 0 for success.
  1713. */
  1714. int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
  1715. struct snd_ctl_elem_info *uinfo)
  1716. {
  1717. struct soc_mixer_control *mc =
  1718. (struct soc_mixer_control *)kcontrol->private_value;
  1719. int max = mc->max;
  1720. if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
  1721. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  1722. else
  1723. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1724. uinfo->count = 2;
  1725. uinfo->value.integer.min = 0;
  1726. uinfo->value.integer.max = max;
  1727. return 0;
  1728. }
  1729. EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
  1730. /**
  1731. * snd_soc_get_volsw_2r - double mixer get callback
  1732. * @kcontrol: mixer control
  1733. * @ucontrol: control element information
  1734. *
  1735. * Callback to get the value of a double mixer control that spans 2 registers.
  1736. *
  1737. * Returns 0 for success.
  1738. */
  1739. int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
  1740. struct snd_ctl_elem_value *ucontrol)
  1741. {
  1742. struct soc_mixer_control *mc =
  1743. (struct soc_mixer_control *)kcontrol->private_value;
  1744. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1745. unsigned int reg = mc->reg;
  1746. unsigned int reg2 = mc->rreg;
  1747. unsigned int shift = mc->shift;
  1748. int max = mc->max;
  1749. unsigned int mask = (1 << fls(max)) - 1;
  1750. unsigned int invert = mc->invert;
  1751. ucontrol->value.integer.value[0] =
  1752. (snd_soc_read(codec, reg) >> shift) & mask;
  1753. ucontrol->value.integer.value[1] =
  1754. (snd_soc_read(codec, reg2) >> shift) & mask;
  1755. if (invert) {
  1756. ucontrol->value.integer.value[0] =
  1757. max - ucontrol->value.integer.value[0];
  1758. ucontrol->value.integer.value[1] =
  1759. max - ucontrol->value.integer.value[1];
  1760. }
  1761. return 0;
  1762. }
  1763. EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
  1764. /**
  1765. * snd_soc_put_volsw_2r - double mixer set callback
  1766. * @kcontrol: mixer control
  1767. * @ucontrol: control element information
  1768. *
  1769. * Callback to set the value of a double mixer control that spans 2 registers.
  1770. *
  1771. * Returns 0 for success.
  1772. */
  1773. int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
  1774. struct snd_ctl_elem_value *ucontrol)
  1775. {
  1776. struct soc_mixer_control *mc =
  1777. (struct soc_mixer_control *)kcontrol->private_value;
  1778. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1779. unsigned int reg = mc->reg;
  1780. unsigned int reg2 = mc->rreg;
  1781. unsigned int shift = mc->shift;
  1782. int max = mc->max;
  1783. unsigned int mask = (1 << fls(max)) - 1;
  1784. unsigned int invert = mc->invert;
  1785. int err;
  1786. unsigned int val, val2, val_mask;
  1787. val_mask = mask << shift;
  1788. val = (ucontrol->value.integer.value[0] & mask);
  1789. val2 = (ucontrol->value.integer.value[1] & mask);
  1790. if (invert) {
  1791. val = max - val;
  1792. val2 = max - val2;
  1793. }
  1794. val = val << shift;
  1795. val2 = val2 << shift;
  1796. err = snd_soc_update_bits(codec, reg, val_mask, val);
  1797. if (err < 0)
  1798. return err;
  1799. err = snd_soc_update_bits(codec, reg2, val_mask, val2);
  1800. return err;
  1801. }
  1802. EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
  1803. /**
  1804. * snd_soc_info_volsw_s8 - signed mixer info callback
  1805. * @kcontrol: mixer control
  1806. * @uinfo: control element information
  1807. *
  1808. * Callback to provide information about a signed mixer control.
  1809. *
  1810. * Returns 0 for success.
  1811. */
  1812. int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
  1813. struct snd_ctl_elem_info *uinfo)
  1814. {
  1815. struct soc_mixer_control *mc =
  1816. (struct soc_mixer_control *)kcontrol->private_value;
  1817. int max = mc->max;
  1818. int min = mc->min;
  1819. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1820. uinfo->count = 2;
  1821. uinfo->value.integer.min = 0;
  1822. uinfo->value.integer.max = max-min;
  1823. return 0;
  1824. }
  1825. EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
  1826. /**
  1827. * snd_soc_get_volsw_s8 - signed mixer get callback
  1828. * @kcontrol: mixer control
  1829. * @ucontrol: control element information
  1830. *
  1831. * Callback to get the value of a signed mixer control.
  1832. *
  1833. * Returns 0 for success.
  1834. */
  1835. int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
  1836. struct snd_ctl_elem_value *ucontrol)
  1837. {
  1838. struct soc_mixer_control *mc =
  1839. (struct soc_mixer_control *)kcontrol->private_value;
  1840. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1841. unsigned int reg = mc->reg;
  1842. int min = mc->min;
  1843. int val = snd_soc_read(codec, reg);
  1844. ucontrol->value.integer.value[0] =
  1845. ((signed char)(val & 0xff))-min;
  1846. ucontrol->value.integer.value[1] =
  1847. ((signed char)((val >> 8) & 0xff))-min;
  1848. return 0;
  1849. }
  1850. EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
  1851. /**
  1852. * snd_soc_put_volsw_sgn - signed mixer put callback
  1853. * @kcontrol: mixer control
  1854. * @ucontrol: control element information
  1855. *
  1856. * Callback to set the value of a signed mixer control.
  1857. *
  1858. * Returns 0 for success.
  1859. */
  1860. int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
  1861. struct snd_ctl_elem_value *ucontrol)
  1862. {
  1863. struct soc_mixer_control *mc =
  1864. (struct soc_mixer_control *)kcontrol->private_value;
  1865. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1866. unsigned int reg = mc->reg;
  1867. int min = mc->min;
  1868. unsigned int val;
  1869. val = (ucontrol->value.integer.value[0]+min) & 0xff;
  1870. val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
  1871. return snd_soc_update_bits(codec, reg, 0xffff, val);
  1872. }
  1873. EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
  1874. /**
  1875. * snd_soc_dai_set_sysclk - configure DAI system or master clock.
  1876. * @dai: DAI
  1877. * @clk_id: DAI specific clock ID
  1878. * @freq: new clock frequency in Hz
  1879. * @dir: new clock direction - input/output.
  1880. *
  1881. * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
  1882. */
  1883. int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
  1884. unsigned int freq, int dir)
  1885. {
  1886. if (dai->ops && dai->ops->set_sysclk)
  1887. return dai->ops->set_sysclk(dai, clk_id, freq, dir);
  1888. else
  1889. return -EINVAL;
  1890. }
  1891. EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
  1892. /**
  1893. * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
  1894. * @dai: DAI
  1895. * @div_id: DAI specific clock divider ID
  1896. * @div: new clock divisor.
  1897. *
  1898. * Configures the clock dividers. This is used to derive the best DAI bit and
  1899. * frame clocks from the system or master clock. It's best to set the DAI bit
  1900. * and frame clocks as low as possible to save system power.
  1901. */
  1902. int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
  1903. int div_id, int div)
  1904. {
  1905. if (dai->ops && dai->ops->set_clkdiv)
  1906. return dai->ops->set_clkdiv(dai, div_id, div);
  1907. else
  1908. return -EINVAL;
  1909. }
  1910. EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
  1911. /**
  1912. * snd_soc_dai_set_pll - configure DAI PLL.
  1913. * @dai: DAI
  1914. * @pll_id: DAI specific PLL ID
  1915. * @freq_in: PLL input clock frequency in Hz
  1916. * @freq_out: requested PLL output clock frequency in Hz
  1917. *
  1918. * Configures and enables PLL to generate output clock based on input clock.
  1919. */
  1920. int snd_soc_dai_set_pll(struct snd_soc_dai *dai,
  1921. int pll_id, unsigned int freq_in, unsigned int freq_out)
  1922. {
  1923. if (dai->ops && dai->ops->set_pll)
  1924. return dai->ops->set_pll(dai, pll_id, freq_in, freq_out);
  1925. else
  1926. return -EINVAL;
  1927. }
  1928. EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
  1929. /**
  1930. * snd_soc_dai_set_fmt - configure DAI hardware audio format.
  1931. * @dai: DAI
  1932. * @fmt: SND_SOC_DAIFMT_ format value.
  1933. *
  1934. * Configures the DAI hardware format and clocking.
  1935. */
  1936. int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  1937. {
  1938. if (dai->ops && dai->ops->set_fmt)
  1939. return dai->ops->set_fmt(dai, fmt);
  1940. else
  1941. return -EINVAL;
  1942. }
  1943. EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
  1944. /**
  1945. * snd_soc_dai_set_tdm_slot - configure DAI TDM.
  1946. * @dai: DAI
  1947. * @mask: DAI specific mask representing used slots.
  1948. * @slots: Number of slots in use.
  1949. *
  1950. * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
  1951. * specific.
  1952. */
  1953. int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
  1954. unsigned int mask, int slots)
  1955. {
  1956. if (dai->ops && dai->ops->set_tdm_slot)
  1957. return dai->ops->set_tdm_slot(dai, mask, slots);
  1958. else
  1959. return -EINVAL;
  1960. }
  1961. EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
  1962. /**
  1963. * snd_soc_dai_set_tristate - configure DAI system or master clock.
  1964. * @dai: DAI
  1965. * @tristate: tristate enable
  1966. *
  1967. * Tristates the DAI so that others can use it.
  1968. */
  1969. int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
  1970. {
  1971. if (dai->ops && dai->ops->set_tristate)
  1972. return dai->ops->set_tristate(dai, tristate);
  1973. else
  1974. return -EINVAL;
  1975. }
  1976. EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
  1977. /**
  1978. * snd_soc_dai_digital_mute - configure DAI system or master clock.
  1979. * @dai: DAI
  1980. * @mute: mute enable
  1981. *
  1982. * Mutes the DAI DAC.
  1983. */
  1984. int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
  1985. {
  1986. if (dai->ops && dai->ops->digital_mute)
  1987. return dai->ops->digital_mute(dai, mute);
  1988. else
  1989. return -EINVAL;
  1990. }
  1991. EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
  1992. /**
  1993. * snd_soc_register_card - Register a card with the ASoC core
  1994. *
  1995. * @card: Card to register
  1996. *
  1997. * Note that currently this is an internal only function: it will be
  1998. * exposed to machine drivers after further backporting of ASoC v2
  1999. * registration APIs.
  2000. */
  2001. static int snd_soc_register_card(struct snd_soc_card *card)
  2002. {
  2003. if (!card->name || !card->dev)
  2004. return -EINVAL;
  2005. INIT_LIST_HEAD(&card->list);
  2006. card->instantiated = 0;
  2007. mutex_lock(&client_mutex);
  2008. list_add(&card->list, &card_list);
  2009. snd_soc_instantiate_cards();
  2010. mutex_unlock(&client_mutex);
  2011. dev_dbg(card->dev, "Registered card '%s'\n", card->name);
  2012. return 0;
  2013. }
  2014. /**
  2015. * snd_soc_unregister_card - Unregister a card with the ASoC core
  2016. *
  2017. * @card: Card to unregister
  2018. *
  2019. * Note that currently this is an internal only function: it will be
  2020. * exposed to machine drivers after further backporting of ASoC v2
  2021. * registration APIs.
  2022. */
  2023. static int snd_soc_unregister_card(struct snd_soc_card *card)
  2024. {
  2025. mutex_lock(&client_mutex);
  2026. list_del(&card->list);
  2027. mutex_unlock(&client_mutex);
  2028. dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
  2029. return 0;
  2030. }
  2031. static struct snd_soc_dai_ops null_dai_ops = {
  2032. };
  2033. /**
  2034. * snd_soc_register_dai - Register a DAI with the ASoC core
  2035. *
  2036. * @dai: DAI to register
  2037. */
  2038. int snd_soc_register_dai(struct snd_soc_dai *dai)
  2039. {
  2040. if (!dai->name)
  2041. return -EINVAL;
  2042. /* The device should become mandatory over time */
  2043. if (!dai->dev)
  2044. printk(KERN_WARNING "No device for DAI %s\n", dai->name);
  2045. if (!dai->ops)
  2046. dai->ops = &null_dai_ops;
  2047. INIT_LIST_HEAD(&dai->list);
  2048. mutex_lock(&client_mutex);
  2049. list_add(&dai->list, &dai_list);
  2050. snd_soc_instantiate_cards();
  2051. mutex_unlock(&client_mutex);
  2052. pr_debug("Registered DAI '%s'\n", dai->name);
  2053. return 0;
  2054. }
  2055. EXPORT_SYMBOL_GPL(snd_soc_register_dai);
  2056. /**
  2057. * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
  2058. *
  2059. * @dai: DAI to unregister
  2060. */
  2061. void snd_soc_unregister_dai(struct snd_soc_dai *dai)
  2062. {
  2063. mutex_lock(&client_mutex);
  2064. list_del(&dai->list);
  2065. mutex_unlock(&client_mutex);
  2066. pr_debug("Unregistered DAI '%s'\n", dai->name);
  2067. }
  2068. EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
  2069. /**
  2070. * snd_soc_register_dais - Register multiple DAIs with the ASoC core
  2071. *
  2072. * @dai: Array of DAIs to register
  2073. * @count: Number of DAIs
  2074. */
  2075. int snd_soc_register_dais(struct snd_soc_dai *dai, size_t count)
  2076. {
  2077. int i, ret;
  2078. for (i = 0; i < count; i++) {
  2079. ret = snd_soc_register_dai(&dai[i]);
  2080. if (ret != 0)
  2081. goto err;
  2082. }
  2083. return 0;
  2084. err:
  2085. for (i--; i >= 0; i--)
  2086. snd_soc_unregister_dai(&dai[i]);
  2087. return ret;
  2088. }
  2089. EXPORT_SYMBOL_GPL(snd_soc_register_dais);
  2090. /**
  2091. * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
  2092. *
  2093. * @dai: Array of DAIs to unregister
  2094. * @count: Number of DAIs
  2095. */
  2096. void snd_soc_unregister_dais(struct snd_soc_dai *dai, size_t count)
  2097. {
  2098. int i;
  2099. for (i = 0; i < count; i++)
  2100. snd_soc_unregister_dai(&dai[i]);
  2101. }
  2102. EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
  2103. /**
  2104. * snd_soc_register_platform - Register a platform with the ASoC core
  2105. *
  2106. * @platform: platform to register
  2107. */
  2108. int snd_soc_register_platform(struct snd_soc_platform *platform)
  2109. {
  2110. if (!platform->name)
  2111. return -EINVAL;
  2112. INIT_LIST_HEAD(&platform->list);
  2113. mutex_lock(&client_mutex);
  2114. list_add(&platform->list, &platform_list);
  2115. snd_soc_instantiate_cards();
  2116. mutex_unlock(&client_mutex);
  2117. pr_debug("Registered platform '%s'\n", platform->name);
  2118. return 0;
  2119. }
  2120. EXPORT_SYMBOL_GPL(snd_soc_register_platform);
  2121. /**
  2122. * snd_soc_unregister_platform - Unregister a platform from the ASoC core
  2123. *
  2124. * @platform: platform to unregister
  2125. */
  2126. void snd_soc_unregister_platform(struct snd_soc_platform *platform)
  2127. {
  2128. mutex_lock(&client_mutex);
  2129. list_del(&platform->list);
  2130. mutex_unlock(&client_mutex);
  2131. pr_debug("Unregistered platform '%s'\n", platform->name);
  2132. }
  2133. EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
  2134. static u64 codec_format_map[] = {
  2135. SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
  2136. SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
  2137. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
  2138. SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
  2139. SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
  2140. SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
  2141. SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
  2142. SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
  2143. SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
  2144. SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
  2145. SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
  2146. SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
  2147. SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
  2148. SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
  2149. SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
  2150. | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
  2151. };
  2152. /* Fix up the DAI formats for endianness: codecs don't actually see
  2153. * the endianness of the data but we're using the CPU format
  2154. * definitions which do need to include endianness so we ensure that
  2155. * codec DAIs always have both big and little endian variants set.
  2156. */
  2157. static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
  2158. {
  2159. int i;
  2160. for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
  2161. if (stream->formats & codec_format_map[i])
  2162. stream->formats |= codec_format_map[i];
  2163. }
  2164. /**
  2165. * snd_soc_register_codec - Register a codec with the ASoC core
  2166. *
  2167. * @codec: codec to register
  2168. */
  2169. int snd_soc_register_codec(struct snd_soc_codec *codec)
  2170. {
  2171. int i;
  2172. if (!codec->name)
  2173. return -EINVAL;
  2174. /* The device should become mandatory over time */
  2175. if (!codec->dev)
  2176. printk(KERN_WARNING "No device for codec %s\n", codec->name);
  2177. INIT_LIST_HEAD(&codec->list);
  2178. for (i = 0; i < codec->num_dai; i++) {
  2179. fixup_codec_formats(&codec->dai[i].playback);
  2180. fixup_codec_formats(&codec->dai[i].capture);
  2181. }
  2182. mutex_lock(&client_mutex);
  2183. list_add(&codec->list, &codec_list);
  2184. snd_soc_instantiate_cards();
  2185. mutex_unlock(&client_mutex);
  2186. pr_debug("Registered codec '%s'\n", codec->name);
  2187. return 0;
  2188. }
  2189. EXPORT_SYMBOL_GPL(snd_soc_register_codec);
  2190. /**
  2191. * snd_soc_unregister_codec - Unregister a codec from the ASoC core
  2192. *
  2193. * @codec: codec to unregister
  2194. */
  2195. void snd_soc_unregister_codec(struct snd_soc_codec *codec)
  2196. {
  2197. mutex_lock(&client_mutex);
  2198. list_del(&codec->list);
  2199. mutex_unlock(&client_mutex);
  2200. pr_debug("Unregistered codec '%s'\n", codec->name);
  2201. }
  2202. EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
  2203. static int __init snd_soc_init(void)
  2204. {
  2205. #ifdef CONFIG_DEBUG_FS
  2206. debugfs_root = debugfs_create_dir("asoc", NULL);
  2207. if (IS_ERR(debugfs_root) || !debugfs_root) {
  2208. printk(KERN_WARNING
  2209. "ASoC: Failed to create debugfs directory\n");
  2210. debugfs_root = NULL;
  2211. }
  2212. #endif
  2213. return platform_driver_register(&soc_driver);
  2214. }
  2215. static void __exit snd_soc_exit(void)
  2216. {
  2217. #ifdef CONFIG_DEBUG_FS
  2218. debugfs_remove_recursive(debugfs_root);
  2219. #endif
  2220. platform_driver_unregister(&soc_driver);
  2221. }
  2222. module_init(snd_soc_init);
  2223. module_exit(snd_soc_exit);
  2224. /* Module information */
  2225. MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
  2226. MODULE_DESCRIPTION("ALSA SoC Core");
  2227. MODULE_LICENSE("GPL");
  2228. MODULE_ALIAS("platform:soc-audio");