soc-core.c 94 KB

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