soc-core.c 94 KB

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