soc-core.c 68 KB

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