soc-core.c 69 KB

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