soc-core.c 71 KB

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