soc-core.c 69 KB

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