soc-core.c 75 KB

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