soc-core.c 65 KB

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