soc-core.c 62 KB

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