soc-core.c 58 KB

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