soc-core.c 68 KB

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