soc-core.c 111 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371
  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/ctype.h>
  33. #include <linux/slab.h>
  34. #include <linux/of.h>
  35. #include <sound/ac97_codec.h>
  36. #include <sound/core.h>
  37. #include <sound/jack.h>
  38. #include <sound/pcm.h>
  39. #include <sound/pcm_params.h>
  40. #include <sound/soc.h>
  41. #include <sound/soc-dpcm.h>
  42. #include <sound/initval.h>
  43. #define CREATE_TRACE_POINTS
  44. #include <trace/events/asoc.h>
  45. #define NAME_SIZE 32
  46. static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
  47. #ifdef CONFIG_DEBUG_FS
  48. struct dentry *snd_soc_debugfs_root;
  49. EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
  50. #endif
  51. static DEFINE_MUTEX(client_mutex);
  52. static LIST_HEAD(dai_list);
  53. static LIST_HEAD(platform_list);
  54. static LIST_HEAD(codec_list);
  55. /*
  56. * This is a timeout to do a DAPM powerdown after a stream is closed().
  57. * It can be used to eliminate pops between different playback streams, e.g.
  58. * between two audio tracks.
  59. */
  60. static int pmdown_time = 5000;
  61. module_param(pmdown_time, int, 0);
  62. MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
  63. /* returns the minimum number of bytes needed to represent
  64. * a particular given value */
  65. static int min_bytes_needed(unsigned long val)
  66. {
  67. int c = 0;
  68. int i;
  69. for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c)
  70. if (val & (1UL << i))
  71. break;
  72. c = (sizeof val * 8) - c;
  73. if (!c || (c % 8))
  74. c = (c + 8) / 8;
  75. else
  76. c /= 8;
  77. return c;
  78. }
  79. /* fill buf which is 'len' bytes with a formatted
  80. * string of the form 'reg: value\n' */
  81. static int format_register_str(struct snd_soc_codec *codec,
  82. unsigned int reg, char *buf, size_t len)
  83. {
  84. int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
  85. int regsize = codec->driver->reg_word_size * 2;
  86. int ret;
  87. char tmpbuf[len + 1];
  88. char regbuf[regsize + 1];
  89. /* since tmpbuf is allocated on the stack, warn the callers if they
  90. * try to abuse this function */
  91. WARN_ON(len > 63);
  92. /* +2 for ': ' and + 1 for '\n' */
  93. if (wordsize + regsize + 2 + 1 != len)
  94. return -EINVAL;
  95. ret = snd_soc_read(codec, reg);
  96. if (ret < 0) {
  97. memset(regbuf, 'X', regsize);
  98. regbuf[regsize] = '\0';
  99. } else {
  100. snprintf(regbuf, regsize + 1, "%.*x", regsize, ret);
  101. }
  102. /* prepare the buffer */
  103. snprintf(tmpbuf, len + 1, "%.*x: %s\n", wordsize, reg, regbuf);
  104. /* copy it back to the caller without the '\0' */
  105. memcpy(buf, tmpbuf, len);
  106. return 0;
  107. }
  108. /* codec register dump */
  109. static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf,
  110. size_t count, loff_t pos)
  111. {
  112. int i, step = 1;
  113. int wordsize, regsize;
  114. int len;
  115. size_t total = 0;
  116. loff_t p = 0;
  117. wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
  118. regsize = codec->driver->reg_word_size * 2;
  119. len = wordsize + regsize + 2 + 1;
  120. if (!codec->driver->reg_cache_size)
  121. return 0;
  122. if (codec->driver->reg_cache_step)
  123. step = codec->driver->reg_cache_step;
  124. for (i = 0; i < codec->driver->reg_cache_size; i += step) {
  125. if (!snd_soc_codec_readable_register(codec, i))
  126. continue;
  127. if (codec->driver->display_register) {
  128. count += codec->driver->display_register(codec, buf + count,
  129. PAGE_SIZE - count, i);
  130. } else {
  131. /* only support larger than PAGE_SIZE bytes debugfs
  132. * entries for the default case */
  133. if (p >= pos) {
  134. if (total + len >= count - 1)
  135. break;
  136. format_register_str(codec, i, buf + total, len);
  137. total += len;
  138. }
  139. p += len;
  140. }
  141. }
  142. total = min(total, count - 1);
  143. return total;
  144. }
  145. static ssize_t codec_reg_show(struct device *dev,
  146. struct device_attribute *attr, char *buf)
  147. {
  148. struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
  149. return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
  150. }
  151. static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
  152. static ssize_t pmdown_time_show(struct device *dev,
  153. struct device_attribute *attr, char *buf)
  154. {
  155. struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
  156. return sprintf(buf, "%ld\n", rtd->pmdown_time);
  157. }
  158. static ssize_t pmdown_time_set(struct device *dev,
  159. struct device_attribute *attr,
  160. const char *buf, size_t count)
  161. {
  162. struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
  163. int ret;
  164. ret = strict_strtol(buf, 10, &rtd->pmdown_time);
  165. if (ret)
  166. return ret;
  167. return count;
  168. }
  169. static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
  170. #ifdef CONFIG_DEBUG_FS
  171. static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
  172. size_t count, loff_t *ppos)
  173. {
  174. ssize_t ret;
  175. struct snd_soc_codec *codec = file->private_data;
  176. char *buf;
  177. if (*ppos < 0 || !count)
  178. return -EINVAL;
  179. buf = kmalloc(count, GFP_KERNEL);
  180. if (!buf)
  181. return -ENOMEM;
  182. ret = soc_codec_reg_show(codec, buf, count, *ppos);
  183. if (ret >= 0) {
  184. if (copy_to_user(user_buf, buf, ret)) {
  185. kfree(buf);
  186. return -EFAULT;
  187. }
  188. *ppos += ret;
  189. }
  190. kfree(buf);
  191. return ret;
  192. }
  193. static ssize_t codec_reg_write_file(struct file *file,
  194. const char __user *user_buf, size_t count, loff_t *ppos)
  195. {
  196. char buf[32];
  197. size_t buf_size;
  198. char *start = buf;
  199. unsigned long reg, value;
  200. struct snd_soc_codec *codec = file->private_data;
  201. buf_size = min(count, (sizeof(buf)-1));
  202. if (copy_from_user(buf, user_buf, buf_size))
  203. return -EFAULT;
  204. buf[buf_size] = 0;
  205. while (*start == ' ')
  206. start++;
  207. reg = simple_strtoul(start, &start, 16);
  208. while (*start == ' ')
  209. start++;
  210. if (strict_strtoul(start, 16, &value))
  211. return -EINVAL;
  212. /* Userspace has been fiddling around behind the kernel's back */
  213. add_taint(TAINT_USER, LOCKDEP_NOW_UNRELIABLE);
  214. snd_soc_write(codec, reg, value);
  215. return buf_size;
  216. }
  217. static const struct file_operations codec_reg_fops = {
  218. .open = simple_open,
  219. .read = codec_reg_read_file,
  220. .write = codec_reg_write_file,
  221. .llseek = default_llseek,
  222. };
  223. static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
  224. {
  225. struct dentry *debugfs_card_root = codec->card->debugfs_card_root;
  226. codec->debugfs_codec_root = debugfs_create_dir(codec->name,
  227. debugfs_card_root);
  228. if (!codec->debugfs_codec_root) {
  229. dev_warn(codec->dev, "ASoC: Failed to create codec debugfs"
  230. " directory\n");
  231. return;
  232. }
  233. debugfs_create_bool("cache_sync", 0444, codec->debugfs_codec_root,
  234. &codec->cache_sync);
  235. debugfs_create_bool("cache_only", 0444, codec->debugfs_codec_root,
  236. &codec->cache_only);
  237. codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
  238. codec->debugfs_codec_root,
  239. codec, &codec_reg_fops);
  240. if (!codec->debugfs_reg)
  241. dev_warn(codec->dev, "ASoC: Failed to create codec register"
  242. " debugfs file\n");
  243. snd_soc_dapm_debugfs_init(&codec->dapm, codec->debugfs_codec_root);
  244. }
  245. static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
  246. {
  247. debugfs_remove_recursive(codec->debugfs_codec_root);
  248. }
  249. static void soc_init_platform_debugfs(struct snd_soc_platform *platform)
  250. {
  251. struct dentry *debugfs_card_root = platform->card->debugfs_card_root;
  252. platform->debugfs_platform_root = debugfs_create_dir(platform->name,
  253. debugfs_card_root);
  254. if (!platform->debugfs_platform_root) {
  255. dev_warn(platform->dev,
  256. "ASoC: Failed to create platform debugfs directory\n");
  257. return;
  258. }
  259. snd_soc_dapm_debugfs_init(&platform->dapm,
  260. platform->debugfs_platform_root);
  261. }
  262. static void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
  263. {
  264. debugfs_remove_recursive(platform->debugfs_platform_root);
  265. }
  266. static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
  267. size_t count, loff_t *ppos)
  268. {
  269. char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  270. ssize_t len, ret = 0;
  271. struct snd_soc_codec *codec;
  272. if (!buf)
  273. return -ENOMEM;
  274. list_for_each_entry(codec, &codec_list, list) {
  275. len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
  276. codec->name);
  277. if (len >= 0)
  278. ret += len;
  279. if (ret > PAGE_SIZE) {
  280. ret = PAGE_SIZE;
  281. break;
  282. }
  283. }
  284. if (ret >= 0)
  285. ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
  286. kfree(buf);
  287. return ret;
  288. }
  289. static const struct file_operations codec_list_fops = {
  290. .read = codec_list_read_file,
  291. .llseek = default_llseek,/* read accesses f_pos */
  292. };
  293. static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
  294. size_t count, loff_t *ppos)
  295. {
  296. char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  297. ssize_t len, ret = 0;
  298. struct snd_soc_dai *dai;
  299. if (!buf)
  300. return -ENOMEM;
  301. list_for_each_entry(dai, &dai_list, list) {
  302. len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", dai->name);
  303. if (len >= 0)
  304. ret += len;
  305. if (ret > PAGE_SIZE) {
  306. ret = PAGE_SIZE;
  307. break;
  308. }
  309. }
  310. ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
  311. kfree(buf);
  312. return ret;
  313. }
  314. static const struct file_operations dai_list_fops = {
  315. .read = dai_list_read_file,
  316. .llseek = default_llseek,/* read accesses f_pos */
  317. };
  318. static ssize_t platform_list_read_file(struct file *file,
  319. char __user *user_buf,
  320. size_t count, loff_t *ppos)
  321. {
  322. char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  323. ssize_t len, ret = 0;
  324. struct snd_soc_platform *platform;
  325. if (!buf)
  326. return -ENOMEM;
  327. list_for_each_entry(platform, &platform_list, list) {
  328. len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
  329. platform->name);
  330. if (len >= 0)
  331. ret += len;
  332. if (ret > PAGE_SIZE) {
  333. ret = PAGE_SIZE;
  334. break;
  335. }
  336. }
  337. ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
  338. kfree(buf);
  339. return ret;
  340. }
  341. static const struct file_operations platform_list_fops = {
  342. .read = platform_list_read_file,
  343. .llseek = default_llseek,/* read accesses f_pos */
  344. };
  345. static void soc_init_card_debugfs(struct snd_soc_card *card)
  346. {
  347. card->debugfs_card_root = debugfs_create_dir(card->name,
  348. snd_soc_debugfs_root);
  349. if (!card->debugfs_card_root) {
  350. dev_warn(card->dev,
  351. "ASoC: Failed to create card debugfs directory\n");
  352. return;
  353. }
  354. card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
  355. card->debugfs_card_root,
  356. &card->pop_time);
  357. if (!card->debugfs_pop_time)
  358. dev_warn(card->dev,
  359. "ASoC: Failed to create pop time debugfs file\n");
  360. }
  361. static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
  362. {
  363. debugfs_remove_recursive(card->debugfs_card_root);
  364. }
  365. #else
  366. static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
  367. {
  368. }
  369. static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
  370. {
  371. }
  372. static inline void soc_init_platform_debugfs(struct snd_soc_platform *platform)
  373. {
  374. }
  375. static inline void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
  376. {
  377. }
  378. static inline void soc_init_card_debugfs(struct snd_soc_card *card)
  379. {
  380. }
  381. static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
  382. {
  383. }
  384. #endif
  385. struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
  386. const char *dai_link, int stream)
  387. {
  388. int i;
  389. for (i = 0; i < card->num_links; i++) {
  390. if (card->rtd[i].dai_link->no_pcm &&
  391. !strcmp(card->rtd[i].dai_link->name, dai_link))
  392. return card->rtd[i].pcm->streams[stream].substream;
  393. }
  394. dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
  395. return NULL;
  396. }
  397. EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
  398. struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
  399. const char *dai_link)
  400. {
  401. int i;
  402. for (i = 0; i < card->num_links; i++) {
  403. if (!strcmp(card->rtd[i].dai_link->name, dai_link))
  404. return &card->rtd[i];
  405. }
  406. dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
  407. return NULL;
  408. }
  409. EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
  410. #ifdef CONFIG_SND_SOC_AC97_BUS
  411. /* unregister ac97 codec */
  412. static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
  413. {
  414. if (codec->ac97->dev.bus)
  415. device_unregister(&codec->ac97->dev);
  416. return 0;
  417. }
  418. /* stop no dev release warning */
  419. static void soc_ac97_device_release(struct device *dev){}
  420. /* register ac97 codec to bus */
  421. static int soc_ac97_dev_register(struct snd_soc_codec *codec)
  422. {
  423. int err;
  424. codec->ac97->dev.bus = &ac97_bus_type;
  425. codec->ac97->dev.parent = codec->card->dev;
  426. codec->ac97->dev.release = soc_ac97_device_release;
  427. dev_set_name(&codec->ac97->dev, "%d-%d:%s",
  428. codec->card->snd_card->number, 0, codec->name);
  429. err = device_register(&codec->ac97->dev);
  430. if (err < 0) {
  431. dev_err(codec->dev, "ASoC: Can't register ac97 bus\n");
  432. codec->ac97->dev.bus = NULL;
  433. return err;
  434. }
  435. return 0;
  436. }
  437. #endif
  438. #ifdef CONFIG_PM_SLEEP
  439. /* powers down audio subsystem for suspend */
  440. int snd_soc_suspend(struct device *dev)
  441. {
  442. struct snd_soc_card *card = dev_get_drvdata(dev);
  443. struct snd_soc_codec *codec;
  444. int i;
  445. /* If the initialization of this soc device failed, there is no codec
  446. * associated with it. Just bail out in this case.
  447. */
  448. if (list_empty(&card->codec_dev_list))
  449. return 0;
  450. /* Due to the resume being scheduled into a workqueue we could
  451. * suspend before that's finished - wait for it to complete.
  452. */
  453. snd_power_lock(card->snd_card);
  454. snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
  455. snd_power_unlock(card->snd_card);
  456. /* we're going to block userspace touching us until resume completes */
  457. snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
  458. /* mute any active DACs */
  459. for (i = 0; i < card->num_rtd; i++) {
  460. struct snd_soc_dai *dai = card->rtd[i].codec_dai;
  461. struct snd_soc_dai_driver *drv = dai->driver;
  462. if (card->rtd[i].dai_link->ignore_suspend)
  463. continue;
  464. if (drv->ops->digital_mute && dai->playback_active)
  465. drv->ops->digital_mute(dai, 1);
  466. }
  467. /* suspend all pcms */
  468. for (i = 0; i < card->num_rtd; i++) {
  469. if (card->rtd[i].dai_link->ignore_suspend)
  470. continue;
  471. snd_pcm_suspend_all(card->rtd[i].pcm);
  472. }
  473. if (card->suspend_pre)
  474. card->suspend_pre(card);
  475. for (i = 0; i < card->num_rtd; i++) {
  476. struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
  477. struct snd_soc_platform *platform = card->rtd[i].platform;
  478. if (card->rtd[i].dai_link->ignore_suspend)
  479. continue;
  480. if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
  481. cpu_dai->driver->suspend(cpu_dai);
  482. if (platform->driver->suspend && !platform->suspended) {
  483. platform->driver->suspend(cpu_dai);
  484. platform->suspended = 1;
  485. }
  486. }
  487. /* close any waiting streams and save state */
  488. for (i = 0; i < card->num_rtd; i++) {
  489. flush_delayed_work(&card->rtd[i].delayed_work);
  490. card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level;
  491. }
  492. for (i = 0; i < card->num_rtd; i++) {
  493. if (card->rtd[i].dai_link->ignore_suspend)
  494. continue;
  495. snd_soc_dapm_stream_event(&card->rtd[i],
  496. SNDRV_PCM_STREAM_PLAYBACK,
  497. SND_SOC_DAPM_STREAM_SUSPEND);
  498. snd_soc_dapm_stream_event(&card->rtd[i],
  499. SNDRV_PCM_STREAM_CAPTURE,
  500. SND_SOC_DAPM_STREAM_SUSPEND);
  501. }
  502. /* Recheck all analogue paths too */
  503. dapm_mark_io_dirty(&card->dapm);
  504. snd_soc_dapm_sync(&card->dapm);
  505. /* suspend all CODECs */
  506. list_for_each_entry(codec, &card->codec_dev_list, card_list) {
  507. /* If there are paths active then the CODEC will be held with
  508. * bias _ON and should not be suspended. */
  509. if (!codec->suspended && codec->driver->suspend) {
  510. switch (codec->dapm.bias_level) {
  511. case SND_SOC_BIAS_STANDBY:
  512. /*
  513. * If the CODEC is capable of idle
  514. * bias off then being in STANDBY
  515. * means it's doing something,
  516. * otherwise fall through.
  517. */
  518. if (codec->dapm.idle_bias_off) {
  519. dev_dbg(codec->dev,
  520. "ASoC: idle_bias_off CODEC on"
  521. " over suspend\n");
  522. break;
  523. }
  524. case SND_SOC_BIAS_OFF:
  525. codec->driver->suspend(codec);
  526. codec->suspended = 1;
  527. codec->cache_sync = 1;
  528. if (codec->using_regmap)
  529. regcache_mark_dirty(codec->control_data);
  530. break;
  531. default:
  532. dev_dbg(codec->dev, "ASoC: CODEC is on"
  533. " over suspend\n");
  534. break;
  535. }
  536. }
  537. }
  538. for (i = 0; i < card->num_rtd; i++) {
  539. struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
  540. if (card->rtd[i].dai_link->ignore_suspend)
  541. continue;
  542. if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
  543. cpu_dai->driver->suspend(cpu_dai);
  544. }
  545. if (card->suspend_post)
  546. card->suspend_post(card);
  547. return 0;
  548. }
  549. EXPORT_SYMBOL_GPL(snd_soc_suspend);
  550. /* deferred resume work, so resume can complete before we finished
  551. * setting our codec back up, which can be very slow on I2C
  552. */
  553. static void soc_resume_deferred(struct work_struct *work)
  554. {
  555. struct snd_soc_card *card =
  556. container_of(work, struct snd_soc_card, deferred_resume_work);
  557. struct snd_soc_codec *codec;
  558. int i;
  559. /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
  560. * so userspace apps are blocked from touching us
  561. */
  562. dev_dbg(card->dev, "ASoC: starting resume work\n");
  563. /* Bring us up into D2 so that DAPM starts enabling things */
  564. snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
  565. if (card->resume_pre)
  566. card->resume_pre(card);
  567. /* resume AC97 DAIs */
  568. for (i = 0; i < card->num_rtd; i++) {
  569. struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
  570. if (card->rtd[i].dai_link->ignore_suspend)
  571. continue;
  572. if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
  573. cpu_dai->driver->resume(cpu_dai);
  574. }
  575. list_for_each_entry(codec, &card->codec_dev_list, card_list) {
  576. /* If the CODEC was idle over suspend then it will have been
  577. * left with bias OFF or STANDBY and suspended so we must now
  578. * resume. Otherwise the suspend was suppressed.
  579. */
  580. if (codec->driver->resume && codec->suspended) {
  581. switch (codec->dapm.bias_level) {
  582. case SND_SOC_BIAS_STANDBY:
  583. case SND_SOC_BIAS_OFF:
  584. codec->driver->resume(codec);
  585. codec->suspended = 0;
  586. break;
  587. default:
  588. dev_dbg(codec->dev, "ASoC: CODEC was on over"
  589. " suspend\n");
  590. break;
  591. }
  592. }
  593. }
  594. for (i = 0; i < card->num_rtd; i++) {
  595. if (card->rtd[i].dai_link->ignore_suspend)
  596. continue;
  597. snd_soc_dapm_stream_event(&card->rtd[i],
  598. SNDRV_PCM_STREAM_PLAYBACK,
  599. SND_SOC_DAPM_STREAM_RESUME);
  600. snd_soc_dapm_stream_event(&card->rtd[i],
  601. SNDRV_PCM_STREAM_CAPTURE,
  602. SND_SOC_DAPM_STREAM_RESUME);
  603. }
  604. /* unmute any active DACs */
  605. for (i = 0; i < card->num_rtd; i++) {
  606. struct snd_soc_dai *dai = card->rtd[i].codec_dai;
  607. struct snd_soc_dai_driver *drv = dai->driver;
  608. if (card->rtd[i].dai_link->ignore_suspend)
  609. continue;
  610. if (drv->ops->digital_mute && dai->playback_active)
  611. drv->ops->digital_mute(dai, 0);
  612. }
  613. for (i = 0; i < card->num_rtd; i++) {
  614. struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
  615. struct snd_soc_platform *platform = card->rtd[i].platform;
  616. if (card->rtd[i].dai_link->ignore_suspend)
  617. continue;
  618. if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
  619. cpu_dai->driver->resume(cpu_dai);
  620. if (platform->driver->resume && platform->suspended) {
  621. platform->driver->resume(cpu_dai);
  622. platform->suspended = 0;
  623. }
  624. }
  625. if (card->resume_post)
  626. card->resume_post(card);
  627. dev_dbg(card->dev, "ASoC: resume work completed\n");
  628. /* userspace can access us now we are back as we were before */
  629. snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
  630. /* Recheck all analogue paths too */
  631. dapm_mark_io_dirty(&card->dapm);
  632. snd_soc_dapm_sync(&card->dapm);
  633. }
  634. /* powers up audio subsystem after a suspend */
  635. int snd_soc_resume(struct device *dev)
  636. {
  637. struct snd_soc_card *card = dev_get_drvdata(dev);
  638. int i, ac97_control = 0;
  639. /* If the initialization of this soc device failed, there is no codec
  640. * associated with it. Just bail out in this case.
  641. */
  642. if (list_empty(&card->codec_dev_list))
  643. return 0;
  644. /* AC97 devices might have other drivers hanging off them so
  645. * need to resume immediately. Other drivers don't have that
  646. * problem and may take a substantial amount of time to resume
  647. * due to I/O costs and anti-pop so handle them out of line.
  648. */
  649. for (i = 0; i < card->num_rtd; i++) {
  650. struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
  651. ac97_control |= cpu_dai->driver->ac97_control;
  652. }
  653. if (ac97_control) {
  654. dev_dbg(dev, "ASoC: Resuming AC97 immediately\n");
  655. soc_resume_deferred(&card->deferred_resume_work);
  656. } else {
  657. dev_dbg(dev, "ASoC: Scheduling resume work\n");
  658. if (!schedule_work(&card->deferred_resume_work))
  659. dev_err(dev, "ASoC: resume work item may be lost\n");
  660. }
  661. return 0;
  662. }
  663. EXPORT_SYMBOL_GPL(snd_soc_resume);
  664. #else
  665. #define snd_soc_suspend NULL
  666. #define snd_soc_resume NULL
  667. #endif
  668. static const struct snd_soc_dai_ops null_dai_ops = {
  669. };
  670. static int soc_bind_dai_link(struct snd_soc_card *card, int num)
  671. {
  672. struct snd_soc_dai_link *dai_link = &card->dai_link[num];
  673. struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
  674. struct snd_soc_codec *codec;
  675. struct snd_soc_platform *platform;
  676. struct snd_soc_dai *codec_dai, *cpu_dai;
  677. const char *platform_name;
  678. dev_dbg(card->dev, "ASoC: binding %s at idx %d\n", dai_link->name, num);
  679. /* Find CPU DAI from registered DAIs*/
  680. list_for_each_entry(cpu_dai, &dai_list, list) {
  681. if (dai_link->cpu_of_node &&
  682. (cpu_dai->dev->of_node != dai_link->cpu_of_node))
  683. continue;
  684. if (dai_link->cpu_name &&
  685. strcmp(dev_name(cpu_dai->dev), dai_link->cpu_name))
  686. continue;
  687. if (dai_link->cpu_dai_name &&
  688. strcmp(cpu_dai->name, dai_link->cpu_dai_name))
  689. continue;
  690. rtd->cpu_dai = cpu_dai;
  691. }
  692. if (!rtd->cpu_dai) {
  693. dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
  694. dai_link->cpu_dai_name);
  695. return -EPROBE_DEFER;
  696. }
  697. /* Find CODEC from registered CODECs */
  698. list_for_each_entry(codec, &codec_list, list) {
  699. if (dai_link->codec_of_node) {
  700. if (codec->dev->of_node != dai_link->codec_of_node)
  701. continue;
  702. } else {
  703. if (strcmp(codec->name, dai_link->codec_name))
  704. continue;
  705. }
  706. rtd->codec = codec;
  707. /*
  708. * CODEC found, so find CODEC DAI from registered DAIs from
  709. * this CODEC
  710. */
  711. list_for_each_entry(codec_dai, &dai_list, list) {
  712. if (codec->dev == codec_dai->dev &&
  713. !strcmp(codec_dai->name,
  714. dai_link->codec_dai_name)) {
  715. rtd->codec_dai = codec_dai;
  716. }
  717. }
  718. if (!rtd->codec_dai) {
  719. dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
  720. dai_link->codec_dai_name);
  721. return -EPROBE_DEFER;
  722. }
  723. }
  724. if (!rtd->codec) {
  725. dev_err(card->dev, "ASoC: CODEC %s not registered\n",
  726. dai_link->codec_name);
  727. return -EPROBE_DEFER;
  728. }
  729. /* if there's no platform we match on the empty platform */
  730. platform_name = dai_link->platform_name;
  731. if (!platform_name && !dai_link->platform_of_node)
  732. platform_name = "snd-soc-dummy";
  733. /* find one from the set of registered platforms */
  734. list_for_each_entry(platform, &platform_list, list) {
  735. if (dai_link->platform_of_node) {
  736. if (platform->dev->of_node !=
  737. dai_link->platform_of_node)
  738. continue;
  739. } else {
  740. if (strcmp(platform->name, platform_name))
  741. continue;
  742. }
  743. rtd->platform = platform;
  744. }
  745. if (!rtd->platform) {
  746. dev_err(card->dev, "ASoC: platform %s not registered\n",
  747. dai_link->platform_name);
  748. return -EPROBE_DEFER;
  749. }
  750. card->num_rtd++;
  751. return 0;
  752. }
  753. static int soc_remove_platform(struct snd_soc_platform *platform)
  754. {
  755. int ret;
  756. if (platform->driver->remove) {
  757. ret = platform->driver->remove(platform);
  758. if (ret < 0)
  759. dev_err(platform->dev, "ASoC: failed to remove %d\n",
  760. ret);
  761. }
  762. /* Make sure all DAPM widgets are freed */
  763. snd_soc_dapm_free(&platform->dapm);
  764. soc_cleanup_platform_debugfs(platform);
  765. platform->probed = 0;
  766. list_del(&platform->card_list);
  767. module_put(platform->dev->driver->owner);
  768. return 0;
  769. }
  770. static void soc_remove_codec(struct snd_soc_codec *codec)
  771. {
  772. int err;
  773. if (codec->driver->remove) {
  774. err = codec->driver->remove(codec);
  775. if (err < 0)
  776. dev_err(codec->dev, "ASoC: failed to remove %d\n", err);
  777. }
  778. /* Make sure all DAPM widgets are freed */
  779. snd_soc_dapm_free(&codec->dapm);
  780. soc_cleanup_codec_debugfs(codec);
  781. codec->probed = 0;
  782. list_del(&codec->card_list);
  783. module_put(codec->dev->driver->owner);
  784. }
  785. static void soc_remove_link_dais(struct snd_soc_card *card, int num, int order)
  786. {
  787. struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
  788. struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
  789. int err;
  790. /* unregister the rtd device */
  791. if (rtd->dev_registered) {
  792. device_remove_file(rtd->dev, &dev_attr_pmdown_time);
  793. device_remove_file(rtd->dev, &dev_attr_codec_reg);
  794. device_unregister(rtd->dev);
  795. rtd->dev_registered = 0;
  796. }
  797. /* remove the CODEC DAI */
  798. if (codec_dai && codec_dai->probed &&
  799. codec_dai->driver->remove_order == order) {
  800. if (codec_dai->driver->remove) {
  801. err = codec_dai->driver->remove(codec_dai);
  802. if (err < 0)
  803. dev_err(codec_dai->dev,
  804. "ASoC: failed to remove %s: %d\n",
  805. codec_dai->name, err);
  806. }
  807. codec_dai->probed = 0;
  808. list_del(&codec_dai->card_list);
  809. }
  810. /* remove the cpu_dai */
  811. if (cpu_dai && cpu_dai->probed &&
  812. cpu_dai->driver->remove_order == order) {
  813. if (cpu_dai->driver->remove) {
  814. err = cpu_dai->driver->remove(cpu_dai);
  815. if (err < 0)
  816. dev_err(cpu_dai->dev,
  817. "ASoC: failed to remove %s: %d\n",
  818. cpu_dai->name, err);
  819. }
  820. cpu_dai->probed = 0;
  821. list_del(&cpu_dai->card_list);
  822. if (!cpu_dai->codec) {
  823. snd_soc_dapm_free(&cpu_dai->dapm);
  824. module_put(cpu_dai->dev->driver->owner);
  825. }
  826. }
  827. }
  828. static void soc_remove_link_components(struct snd_soc_card *card, int num,
  829. int order)
  830. {
  831. struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
  832. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  833. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  834. struct snd_soc_platform *platform = rtd->platform;
  835. struct snd_soc_codec *codec;
  836. /* remove the platform */
  837. if (platform && platform->probed &&
  838. platform->driver->remove_order == order) {
  839. soc_remove_platform(platform);
  840. }
  841. /* remove the CODEC-side CODEC */
  842. if (codec_dai) {
  843. codec = codec_dai->codec;
  844. if (codec && codec->probed &&
  845. codec->driver->remove_order == order)
  846. soc_remove_codec(codec);
  847. }
  848. /* remove any CPU-side CODEC */
  849. if (cpu_dai) {
  850. codec = cpu_dai->codec;
  851. if (codec && codec->probed &&
  852. codec->driver->remove_order == order)
  853. soc_remove_codec(codec);
  854. }
  855. }
  856. static void soc_remove_dai_links(struct snd_soc_card *card)
  857. {
  858. int dai, order;
  859. for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
  860. order++) {
  861. for (dai = 0; dai < card->num_rtd; dai++)
  862. soc_remove_link_dais(card, dai, order);
  863. }
  864. for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
  865. order++) {
  866. for (dai = 0; dai < card->num_rtd; dai++)
  867. soc_remove_link_components(card, dai, order);
  868. }
  869. card->num_rtd = 0;
  870. }
  871. static void soc_set_name_prefix(struct snd_soc_card *card,
  872. struct snd_soc_codec *codec)
  873. {
  874. int i;
  875. if (card->codec_conf == NULL)
  876. return;
  877. for (i = 0; i < card->num_configs; i++) {
  878. struct snd_soc_codec_conf *map = &card->codec_conf[i];
  879. if (map->dev_name && !strcmp(codec->name, map->dev_name)) {
  880. codec->name_prefix = map->name_prefix;
  881. break;
  882. }
  883. }
  884. }
  885. static int soc_probe_codec(struct snd_soc_card *card,
  886. struct snd_soc_codec *codec)
  887. {
  888. int ret = 0;
  889. const struct snd_soc_codec_driver *driver = codec->driver;
  890. struct snd_soc_dai *dai;
  891. codec->card = card;
  892. codec->dapm.card = card;
  893. soc_set_name_prefix(card, codec);
  894. if (!try_module_get(codec->dev->driver->owner))
  895. return -ENODEV;
  896. soc_init_codec_debugfs(codec);
  897. if (driver->dapm_widgets)
  898. snd_soc_dapm_new_controls(&codec->dapm, driver->dapm_widgets,
  899. driver->num_dapm_widgets);
  900. /* Create DAPM widgets for each DAI stream */
  901. list_for_each_entry(dai, &dai_list, list) {
  902. if (dai->dev != codec->dev)
  903. continue;
  904. snd_soc_dapm_new_dai_widgets(&codec->dapm, dai);
  905. }
  906. codec->dapm.idle_bias_off = driver->idle_bias_off;
  907. if (driver->probe) {
  908. ret = driver->probe(codec);
  909. if (ret < 0) {
  910. dev_err(codec->dev,
  911. "ASoC: failed to probe CODEC %d\n", ret);
  912. goto err_probe;
  913. }
  914. WARN(codec->dapm.idle_bias_off &&
  915. codec->dapm.bias_level != SND_SOC_BIAS_OFF,
  916. "codec %s can not start from non-off bias"
  917. " with idle_bias_off==1\n", codec->name);
  918. }
  919. /* If the driver didn't set I/O up try regmap */
  920. if (!codec->write && dev_get_regmap(codec->dev, NULL))
  921. snd_soc_codec_set_cache_io(codec, 0, 0, SND_SOC_REGMAP);
  922. if (driver->controls)
  923. snd_soc_add_codec_controls(codec, driver->controls,
  924. driver->num_controls);
  925. if (driver->dapm_routes)
  926. snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes,
  927. driver->num_dapm_routes);
  928. /* mark codec as probed and add to card codec list */
  929. codec->probed = 1;
  930. list_add(&codec->card_list, &card->codec_dev_list);
  931. list_add(&codec->dapm.list, &card->dapm_list);
  932. return 0;
  933. err_probe:
  934. soc_cleanup_codec_debugfs(codec);
  935. module_put(codec->dev->driver->owner);
  936. return ret;
  937. }
  938. static int soc_probe_platform(struct snd_soc_card *card,
  939. struct snd_soc_platform *platform)
  940. {
  941. int ret = 0;
  942. const struct snd_soc_platform_driver *driver = platform->driver;
  943. struct snd_soc_dai *dai;
  944. platform->card = card;
  945. platform->dapm.card = card;
  946. if (!try_module_get(platform->dev->driver->owner))
  947. return -ENODEV;
  948. soc_init_platform_debugfs(platform);
  949. if (driver->dapm_widgets)
  950. snd_soc_dapm_new_controls(&platform->dapm,
  951. driver->dapm_widgets, driver->num_dapm_widgets);
  952. /* Create DAPM widgets for each DAI stream */
  953. list_for_each_entry(dai, &dai_list, list) {
  954. if (dai->dev != platform->dev)
  955. continue;
  956. snd_soc_dapm_new_dai_widgets(&platform->dapm, dai);
  957. }
  958. platform->dapm.idle_bias_off = 1;
  959. if (driver->probe) {
  960. ret = driver->probe(platform);
  961. if (ret < 0) {
  962. dev_err(platform->dev,
  963. "ASoC: failed to probe platform %d\n", ret);
  964. goto err_probe;
  965. }
  966. }
  967. if (driver->controls)
  968. snd_soc_add_platform_controls(platform, driver->controls,
  969. driver->num_controls);
  970. if (driver->dapm_routes)
  971. snd_soc_dapm_add_routes(&platform->dapm, driver->dapm_routes,
  972. driver->num_dapm_routes);
  973. /* mark platform as probed and add to card platform list */
  974. platform->probed = 1;
  975. list_add(&platform->card_list, &card->platform_dev_list);
  976. list_add(&platform->dapm.list, &card->dapm_list);
  977. return 0;
  978. err_probe:
  979. soc_cleanup_platform_debugfs(platform);
  980. module_put(platform->dev->driver->owner);
  981. return ret;
  982. }
  983. static void rtd_release(struct device *dev)
  984. {
  985. kfree(dev);
  986. }
  987. static int soc_post_component_init(struct snd_soc_card *card,
  988. struct snd_soc_codec *codec,
  989. int num, int dailess)
  990. {
  991. struct snd_soc_dai_link *dai_link = NULL;
  992. struct snd_soc_aux_dev *aux_dev = NULL;
  993. struct snd_soc_pcm_runtime *rtd;
  994. const char *temp, *name;
  995. int ret = 0;
  996. if (!dailess) {
  997. dai_link = &card->dai_link[num];
  998. rtd = &card->rtd[num];
  999. name = dai_link->name;
  1000. } else {
  1001. aux_dev = &card->aux_dev[num];
  1002. rtd = &card->rtd_aux[num];
  1003. name = aux_dev->name;
  1004. }
  1005. rtd->card = card;
  1006. /* Make sure all DAPM widgets are instantiated */
  1007. snd_soc_dapm_new_widgets(&codec->dapm);
  1008. /* machine controls, routes and widgets are not prefixed */
  1009. temp = codec->name_prefix;
  1010. codec->name_prefix = NULL;
  1011. /* do machine specific initialization */
  1012. if (!dailess && dai_link->init)
  1013. ret = dai_link->init(rtd);
  1014. else if (dailess && aux_dev->init)
  1015. ret = aux_dev->init(&codec->dapm);
  1016. if (ret < 0) {
  1017. dev_err(card->dev, "ASoC: failed to init %s: %d\n", name, ret);
  1018. return ret;
  1019. }
  1020. codec->name_prefix = temp;
  1021. /* register the rtd device */
  1022. rtd->codec = codec;
  1023. rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
  1024. if (!rtd->dev)
  1025. return -ENOMEM;
  1026. device_initialize(rtd->dev);
  1027. rtd->dev->parent = card->dev;
  1028. rtd->dev->release = rtd_release;
  1029. rtd->dev->init_name = name;
  1030. dev_set_drvdata(rtd->dev, rtd);
  1031. mutex_init(&rtd->pcm_mutex);
  1032. INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
  1033. INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
  1034. INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
  1035. INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
  1036. ret = device_add(rtd->dev);
  1037. if (ret < 0) {
  1038. /* calling put_device() here to free the rtd->dev */
  1039. put_device(rtd->dev);
  1040. dev_err(card->dev,
  1041. "ASoC: failed to register runtime device: %d\n", ret);
  1042. return ret;
  1043. }
  1044. rtd->dev_registered = 1;
  1045. /* add DAPM sysfs entries for this codec */
  1046. ret = snd_soc_dapm_sys_add(rtd->dev);
  1047. if (ret < 0)
  1048. dev_err(codec->dev,
  1049. "ASoC: failed to add codec dapm sysfs entries: %d\n", ret);
  1050. /* add codec sysfs entries */
  1051. ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
  1052. if (ret < 0)
  1053. dev_err(codec->dev,
  1054. "ASoC: failed to add codec sysfs files: %d\n", ret);
  1055. #ifdef CONFIG_DEBUG_FS
  1056. /* add DPCM sysfs entries */
  1057. if (!dailess && !dai_link->dynamic)
  1058. goto out;
  1059. ret = soc_dpcm_debugfs_add(rtd);
  1060. if (ret < 0)
  1061. dev_err(rtd->dev, "ASoC: failed to add dpcm sysfs entries: %d\n", ret);
  1062. out:
  1063. #endif
  1064. return 0;
  1065. }
  1066. static int soc_probe_link_components(struct snd_soc_card *card, int num,
  1067. int order)
  1068. {
  1069. struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
  1070. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  1071. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  1072. struct snd_soc_platform *platform = rtd->platform;
  1073. int ret;
  1074. /* probe the CPU-side component, if it is a CODEC */
  1075. if (cpu_dai->codec &&
  1076. !cpu_dai->codec->probed &&
  1077. cpu_dai->codec->driver->probe_order == order) {
  1078. ret = soc_probe_codec(card, cpu_dai->codec);
  1079. if (ret < 0)
  1080. return ret;
  1081. }
  1082. /* probe the CODEC-side component */
  1083. if (!codec_dai->codec->probed &&
  1084. codec_dai->codec->driver->probe_order == order) {
  1085. ret = soc_probe_codec(card, codec_dai->codec);
  1086. if (ret < 0)
  1087. return ret;
  1088. }
  1089. /* probe the platform */
  1090. if (!platform->probed &&
  1091. platform->driver->probe_order == order) {
  1092. ret = soc_probe_platform(card, platform);
  1093. if (ret < 0)
  1094. return ret;
  1095. }
  1096. return 0;
  1097. }
  1098. static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order)
  1099. {
  1100. struct snd_soc_dai_link *dai_link = &card->dai_link[num];
  1101. struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
  1102. struct snd_soc_codec *codec = rtd->codec;
  1103. struct snd_soc_platform *platform = rtd->platform;
  1104. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  1105. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  1106. struct snd_soc_dapm_widget *play_w, *capture_w;
  1107. int ret;
  1108. dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
  1109. card->name, num, order);
  1110. /* config components */
  1111. cpu_dai->platform = platform;
  1112. codec_dai->card = card;
  1113. cpu_dai->card = card;
  1114. /* set default power off timeout */
  1115. rtd->pmdown_time = pmdown_time;
  1116. /* probe the cpu_dai */
  1117. if (!cpu_dai->probed &&
  1118. cpu_dai->driver->probe_order == order) {
  1119. if (!cpu_dai->codec) {
  1120. cpu_dai->dapm.card = card;
  1121. if (!try_module_get(cpu_dai->dev->driver->owner))
  1122. return -ENODEV;
  1123. list_add(&cpu_dai->dapm.list, &card->dapm_list);
  1124. snd_soc_dapm_new_dai_widgets(&cpu_dai->dapm, cpu_dai);
  1125. }
  1126. if (cpu_dai->driver->probe) {
  1127. ret = cpu_dai->driver->probe(cpu_dai);
  1128. if (ret < 0) {
  1129. dev_err(cpu_dai->dev,
  1130. "ASoC: failed to probe CPU DAI %s: %d\n",
  1131. cpu_dai->name, ret);
  1132. module_put(cpu_dai->dev->driver->owner);
  1133. return ret;
  1134. }
  1135. }
  1136. cpu_dai->probed = 1;
  1137. /* mark cpu_dai as probed and add to card dai list */
  1138. list_add(&cpu_dai->card_list, &card->dai_dev_list);
  1139. }
  1140. /* probe the CODEC DAI */
  1141. if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
  1142. if (codec_dai->driver->probe) {
  1143. ret = codec_dai->driver->probe(codec_dai);
  1144. if (ret < 0) {
  1145. dev_err(codec_dai->dev,
  1146. "ASoC: failed to probe CODEC DAI %s: %d\n",
  1147. codec_dai->name, ret);
  1148. return ret;
  1149. }
  1150. }
  1151. /* mark codec_dai as probed and add to card dai list */
  1152. codec_dai->probed = 1;
  1153. list_add(&codec_dai->card_list, &card->dai_dev_list);
  1154. }
  1155. /* complete DAI probe during last probe */
  1156. if (order != SND_SOC_COMP_ORDER_LAST)
  1157. return 0;
  1158. ret = soc_post_component_init(card, codec, num, 0);
  1159. if (ret)
  1160. return ret;
  1161. ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
  1162. if (ret < 0)
  1163. dev_warn(rtd->dev, "ASoC: failed to add pmdown_time sysfs: %d\n",
  1164. ret);
  1165. if (cpu_dai->driver->compress_dai) {
  1166. /*create compress_device"*/
  1167. ret = soc_new_compress(rtd, num);
  1168. if (ret < 0) {
  1169. dev_err(card->dev, "ASoC: can't create compress %s\n",
  1170. dai_link->stream_name);
  1171. return ret;
  1172. }
  1173. } else {
  1174. if (!dai_link->params) {
  1175. /* create the pcm */
  1176. ret = soc_new_pcm(rtd, num);
  1177. if (ret < 0) {
  1178. dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
  1179. dai_link->stream_name, ret);
  1180. return ret;
  1181. }
  1182. } else {
  1183. /* link the DAI widgets */
  1184. play_w = codec_dai->playback_widget;
  1185. capture_w = cpu_dai->capture_widget;
  1186. if (play_w && capture_w) {
  1187. ret = snd_soc_dapm_new_pcm(card, dai_link->params,
  1188. capture_w, play_w);
  1189. if (ret != 0) {
  1190. dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
  1191. play_w->name, capture_w->name, ret);
  1192. return ret;
  1193. }
  1194. }
  1195. play_w = cpu_dai->playback_widget;
  1196. capture_w = codec_dai->capture_widget;
  1197. if (play_w && capture_w) {
  1198. ret = snd_soc_dapm_new_pcm(card, dai_link->params,
  1199. capture_w, play_w);
  1200. if (ret != 0) {
  1201. dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
  1202. play_w->name, capture_w->name, ret);
  1203. return ret;
  1204. }
  1205. }
  1206. }
  1207. }
  1208. /* add platform data for AC97 devices */
  1209. if (rtd->codec_dai->driver->ac97_control)
  1210. snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
  1211. return 0;
  1212. }
  1213. #ifdef CONFIG_SND_SOC_AC97_BUS
  1214. static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
  1215. {
  1216. int ret;
  1217. /* Only instantiate AC97 if not already done by the adaptor
  1218. * for the generic AC97 subsystem.
  1219. */
  1220. if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
  1221. /*
  1222. * It is possible that the AC97 device is already registered to
  1223. * the device subsystem. This happens when the device is created
  1224. * via snd_ac97_mixer(). Currently only SoC codec that does so
  1225. * is the generic AC97 glue but others migh emerge.
  1226. *
  1227. * In those cases we don't try to register the device again.
  1228. */
  1229. if (!rtd->codec->ac97_created)
  1230. return 0;
  1231. ret = soc_ac97_dev_register(rtd->codec);
  1232. if (ret < 0) {
  1233. dev_err(rtd->codec->dev,
  1234. "ASoC: AC97 device register failed: %d\n", ret);
  1235. return ret;
  1236. }
  1237. rtd->codec->ac97_registered = 1;
  1238. }
  1239. return 0;
  1240. }
  1241. static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
  1242. {
  1243. if (codec->ac97_registered) {
  1244. soc_ac97_dev_unregister(codec);
  1245. codec->ac97_registered = 0;
  1246. }
  1247. }
  1248. #endif
  1249. static int soc_check_aux_dev(struct snd_soc_card *card, int num)
  1250. {
  1251. struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
  1252. struct snd_soc_codec *codec;
  1253. /* find CODEC from registered CODECs*/
  1254. list_for_each_entry(codec, &codec_list, list) {
  1255. if (!strcmp(codec->name, aux_dev->codec_name))
  1256. return 0;
  1257. }
  1258. dev_err(card->dev, "ASoC: %s not registered\n", aux_dev->codec_name);
  1259. return -EPROBE_DEFER;
  1260. }
  1261. static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
  1262. {
  1263. struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
  1264. struct snd_soc_codec *codec;
  1265. int ret = -ENODEV;
  1266. /* find CODEC from registered CODECs*/
  1267. list_for_each_entry(codec, &codec_list, list) {
  1268. if (!strcmp(codec->name, aux_dev->codec_name)) {
  1269. if (codec->probed) {
  1270. dev_err(codec->dev,
  1271. "ASoC: codec already probed");
  1272. ret = -EBUSY;
  1273. goto out;
  1274. }
  1275. goto found;
  1276. }
  1277. }
  1278. /* codec not found */
  1279. dev_err(card->dev, "ASoC: codec %s not found", aux_dev->codec_name);
  1280. return -EPROBE_DEFER;
  1281. found:
  1282. ret = soc_probe_codec(card, codec);
  1283. if (ret < 0)
  1284. return ret;
  1285. ret = soc_post_component_init(card, codec, num, 1);
  1286. out:
  1287. return ret;
  1288. }
  1289. static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
  1290. {
  1291. struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
  1292. struct snd_soc_codec *codec = rtd->codec;
  1293. /* unregister the rtd device */
  1294. if (rtd->dev_registered) {
  1295. device_remove_file(rtd->dev, &dev_attr_codec_reg);
  1296. device_unregister(rtd->dev);
  1297. rtd->dev_registered = 0;
  1298. }
  1299. if (codec && codec->probed)
  1300. soc_remove_codec(codec);
  1301. }
  1302. static int snd_soc_init_codec_cache(struct snd_soc_codec *codec,
  1303. enum snd_soc_compress_type compress_type)
  1304. {
  1305. int ret;
  1306. if (codec->cache_init)
  1307. return 0;
  1308. /* override the compress_type if necessary */
  1309. if (compress_type && codec->compress_type != compress_type)
  1310. codec->compress_type = compress_type;
  1311. ret = snd_soc_cache_init(codec);
  1312. if (ret < 0) {
  1313. dev_err(codec->dev, "ASoC: Failed to set cache compression"
  1314. " type: %d\n", ret);
  1315. return ret;
  1316. }
  1317. codec->cache_init = 1;
  1318. return 0;
  1319. }
  1320. static int snd_soc_instantiate_card(struct snd_soc_card *card)
  1321. {
  1322. struct snd_soc_codec *codec;
  1323. struct snd_soc_codec_conf *codec_conf;
  1324. enum snd_soc_compress_type compress_type;
  1325. struct snd_soc_dai_link *dai_link;
  1326. int ret, i, order, dai_fmt;
  1327. mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
  1328. /* bind DAIs */
  1329. for (i = 0; i < card->num_links; i++) {
  1330. ret = soc_bind_dai_link(card, i);
  1331. if (ret != 0)
  1332. goto base_error;
  1333. }
  1334. /* check aux_devs too */
  1335. for (i = 0; i < card->num_aux_devs; i++) {
  1336. ret = soc_check_aux_dev(card, i);
  1337. if (ret != 0)
  1338. goto base_error;
  1339. }
  1340. /* initialize the register cache for each available codec */
  1341. list_for_each_entry(codec, &codec_list, list) {
  1342. if (codec->cache_init)
  1343. continue;
  1344. /* by default we don't override the compress_type */
  1345. compress_type = 0;
  1346. /* check to see if we need to override the compress_type */
  1347. for (i = 0; i < card->num_configs; ++i) {
  1348. codec_conf = &card->codec_conf[i];
  1349. if (!strcmp(codec->name, codec_conf->dev_name)) {
  1350. compress_type = codec_conf->compress_type;
  1351. if (compress_type && compress_type
  1352. != codec->compress_type)
  1353. break;
  1354. }
  1355. }
  1356. ret = snd_soc_init_codec_cache(codec, compress_type);
  1357. if (ret < 0)
  1358. goto base_error;
  1359. }
  1360. /* card bind complete so register a sound card */
  1361. ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
  1362. card->owner, 0, &card->snd_card);
  1363. if (ret < 0) {
  1364. dev_err(card->dev, "ASoC: can't create sound card for"
  1365. " card %s: %d\n", card->name, ret);
  1366. goto base_error;
  1367. }
  1368. card->snd_card->dev = card->dev;
  1369. card->dapm.bias_level = SND_SOC_BIAS_OFF;
  1370. card->dapm.dev = card->dev;
  1371. card->dapm.card = card;
  1372. list_add(&card->dapm.list, &card->dapm_list);
  1373. #ifdef CONFIG_DEBUG_FS
  1374. snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
  1375. #endif
  1376. #ifdef CONFIG_PM_SLEEP
  1377. /* deferred resume work */
  1378. INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
  1379. #endif
  1380. if (card->dapm_widgets)
  1381. snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
  1382. card->num_dapm_widgets);
  1383. /* initialise the sound card only once */
  1384. if (card->probe) {
  1385. ret = card->probe(card);
  1386. if (ret < 0)
  1387. goto card_probe_error;
  1388. }
  1389. /* probe all components used by DAI links on this card */
  1390. for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
  1391. order++) {
  1392. for (i = 0; i < card->num_links; i++) {
  1393. ret = soc_probe_link_components(card, i, order);
  1394. if (ret < 0) {
  1395. dev_err(card->dev,
  1396. "ASoC: failed to instantiate card %d\n",
  1397. ret);
  1398. goto probe_dai_err;
  1399. }
  1400. }
  1401. }
  1402. /* probe all DAI links on this card */
  1403. for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
  1404. order++) {
  1405. for (i = 0; i < card->num_links; i++) {
  1406. ret = soc_probe_link_dais(card, i, order);
  1407. if (ret < 0) {
  1408. dev_err(card->dev,
  1409. "ASoC: failed to instantiate card %d\n",
  1410. ret);
  1411. goto probe_dai_err;
  1412. }
  1413. }
  1414. }
  1415. for (i = 0; i < card->num_aux_devs; i++) {
  1416. ret = soc_probe_aux_dev(card, i);
  1417. if (ret < 0) {
  1418. dev_err(card->dev,
  1419. "ASoC: failed to add auxiliary devices %d\n",
  1420. ret);
  1421. goto probe_aux_dev_err;
  1422. }
  1423. }
  1424. snd_soc_dapm_link_dai_widgets(card);
  1425. if (card->controls)
  1426. snd_soc_add_card_controls(card, card->controls, card->num_controls);
  1427. if (card->dapm_routes)
  1428. snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
  1429. card->num_dapm_routes);
  1430. snd_soc_dapm_new_widgets(&card->dapm);
  1431. for (i = 0; i < card->num_links; i++) {
  1432. dai_link = &card->dai_link[i];
  1433. dai_fmt = dai_link->dai_fmt;
  1434. if (dai_fmt) {
  1435. ret = snd_soc_dai_set_fmt(card->rtd[i].codec_dai,
  1436. dai_fmt);
  1437. if (ret != 0 && ret != -ENOTSUPP)
  1438. dev_warn(card->rtd[i].codec_dai->dev,
  1439. "ASoC: Failed to set DAI format: %d\n",
  1440. ret);
  1441. }
  1442. /* If this is a regular CPU link there will be a platform */
  1443. if (dai_fmt &&
  1444. (dai_link->platform_name || dai_link->platform_of_node)) {
  1445. ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
  1446. dai_fmt);
  1447. if (ret != 0 && ret != -ENOTSUPP)
  1448. dev_warn(card->rtd[i].cpu_dai->dev,
  1449. "ASoC: Failed to set DAI format: %d\n",
  1450. ret);
  1451. } else if (dai_fmt) {
  1452. /* Flip the polarity for the "CPU" end */
  1453. dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
  1454. switch (dai_link->dai_fmt &
  1455. SND_SOC_DAIFMT_MASTER_MASK) {
  1456. case SND_SOC_DAIFMT_CBM_CFM:
  1457. dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
  1458. break;
  1459. case SND_SOC_DAIFMT_CBM_CFS:
  1460. dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
  1461. break;
  1462. case SND_SOC_DAIFMT_CBS_CFM:
  1463. dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
  1464. break;
  1465. case SND_SOC_DAIFMT_CBS_CFS:
  1466. dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
  1467. break;
  1468. }
  1469. ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
  1470. dai_fmt);
  1471. if (ret != 0 && ret != -ENOTSUPP)
  1472. dev_warn(card->rtd[i].cpu_dai->dev,
  1473. "ASoC: Failed to set DAI format: %d\n",
  1474. ret);
  1475. }
  1476. }
  1477. snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
  1478. "%s", card->name);
  1479. snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
  1480. "%s", card->long_name ? card->long_name : card->name);
  1481. snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
  1482. "%s", card->driver_name ? card->driver_name : card->name);
  1483. for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
  1484. switch (card->snd_card->driver[i]) {
  1485. case '_':
  1486. case '-':
  1487. case '\0':
  1488. break;
  1489. default:
  1490. if (!isalnum(card->snd_card->driver[i]))
  1491. card->snd_card->driver[i] = '_';
  1492. break;
  1493. }
  1494. }
  1495. if (card->late_probe) {
  1496. ret = card->late_probe(card);
  1497. if (ret < 0) {
  1498. dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
  1499. card->name, ret);
  1500. goto probe_aux_dev_err;
  1501. }
  1502. }
  1503. snd_soc_dapm_new_widgets(&card->dapm);
  1504. if (card->fully_routed)
  1505. list_for_each_entry(codec, &card->codec_dev_list, card_list)
  1506. snd_soc_dapm_auto_nc_codec_pins(codec);
  1507. ret = snd_card_register(card->snd_card);
  1508. if (ret < 0) {
  1509. dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
  1510. ret);
  1511. goto probe_aux_dev_err;
  1512. }
  1513. #ifdef CONFIG_SND_SOC_AC97_BUS
  1514. /* register any AC97 codecs */
  1515. for (i = 0; i < card->num_rtd; i++) {
  1516. ret = soc_register_ac97_dai_link(&card->rtd[i]);
  1517. if (ret < 0) {
  1518. dev_err(card->dev, "ASoC: failed to register AC97:"
  1519. " %d\n", ret);
  1520. while (--i >= 0)
  1521. soc_unregister_ac97_dai_link(card->rtd[i].codec);
  1522. goto probe_aux_dev_err;
  1523. }
  1524. }
  1525. #endif
  1526. card->instantiated = 1;
  1527. snd_soc_dapm_sync(&card->dapm);
  1528. mutex_unlock(&card->mutex);
  1529. return 0;
  1530. probe_aux_dev_err:
  1531. for (i = 0; i < card->num_aux_devs; i++)
  1532. soc_remove_aux_dev(card, i);
  1533. probe_dai_err:
  1534. soc_remove_dai_links(card);
  1535. card_probe_error:
  1536. if (card->remove)
  1537. card->remove(card);
  1538. snd_card_free(card->snd_card);
  1539. base_error:
  1540. mutex_unlock(&card->mutex);
  1541. return ret;
  1542. }
  1543. /* probes a new socdev */
  1544. static int soc_probe(struct platform_device *pdev)
  1545. {
  1546. struct snd_soc_card *card = platform_get_drvdata(pdev);
  1547. /*
  1548. * no card, so machine driver should be registering card
  1549. * we should not be here in that case so ret error
  1550. */
  1551. if (!card)
  1552. return -EINVAL;
  1553. dev_warn(&pdev->dev,
  1554. "ASoC: machine %s should use snd_soc_register_card()\n",
  1555. card->name);
  1556. /* Bodge while we unpick instantiation */
  1557. card->dev = &pdev->dev;
  1558. return snd_soc_register_card(card);
  1559. }
  1560. static int soc_cleanup_card_resources(struct snd_soc_card *card)
  1561. {
  1562. int i;
  1563. /* make sure any delayed work runs */
  1564. for (i = 0; i < card->num_rtd; i++) {
  1565. struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
  1566. flush_delayed_work(&rtd->delayed_work);
  1567. }
  1568. /* remove auxiliary devices */
  1569. for (i = 0; i < card->num_aux_devs; i++)
  1570. soc_remove_aux_dev(card, i);
  1571. /* remove and free each DAI */
  1572. soc_remove_dai_links(card);
  1573. soc_cleanup_card_debugfs(card);
  1574. /* remove the card */
  1575. if (card->remove)
  1576. card->remove(card);
  1577. snd_soc_dapm_free(&card->dapm);
  1578. snd_card_free(card->snd_card);
  1579. return 0;
  1580. }
  1581. /* removes a socdev */
  1582. static int soc_remove(struct platform_device *pdev)
  1583. {
  1584. struct snd_soc_card *card = platform_get_drvdata(pdev);
  1585. snd_soc_unregister_card(card);
  1586. return 0;
  1587. }
  1588. int snd_soc_poweroff(struct device *dev)
  1589. {
  1590. struct snd_soc_card *card = dev_get_drvdata(dev);
  1591. int i;
  1592. if (!card->instantiated)
  1593. return 0;
  1594. /* Flush out pmdown_time work - we actually do want to run it
  1595. * now, we're shutting down so no imminent restart. */
  1596. for (i = 0; i < card->num_rtd; i++) {
  1597. struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
  1598. flush_delayed_work(&rtd->delayed_work);
  1599. }
  1600. snd_soc_dapm_shutdown(card);
  1601. return 0;
  1602. }
  1603. EXPORT_SYMBOL_GPL(snd_soc_poweroff);
  1604. const struct dev_pm_ops snd_soc_pm_ops = {
  1605. .suspend = snd_soc_suspend,
  1606. .resume = snd_soc_resume,
  1607. .freeze = snd_soc_suspend,
  1608. .thaw = snd_soc_resume,
  1609. .poweroff = snd_soc_poweroff,
  1610. .restore = snd_soc_resume,
  1611. };
  1612. EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
  1613. /* ASoC platform driver */
  1614. static struct platform_driver soc_driver = {
  1615. .driver = {
  1616. .name = "soc-audio",
  1617. .owner = THIS_MODULE,
  1618. .pm = &snd_soc_pm_ops,
  1619. },
  1620. .probe = soc_probe,
  1621. .remove = soc_remove,
  1622. };
  1623. /**
  1624. * snd_soc_codec_volatile_register: Report if a register is volatile.
  1625. *
  1626. * @codec: CODEC to query.
  1627. * @reg: Register to query.
  1628. *
  1629. * Boolean function indiciating if a CODEC register is volatile.
  1630. */
  1631. int snd_soc_codec_volatile_register(struct snd_soc_codec *codec,
  1632. unsigned int reg)
  1633. {
  1634. if (codec->volatile_register)
  1635. return codec->volatile_register(codec, reg);
  1636. else
  1637. return 0;
  1638. }
  1639. EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
  1640. /**
  1641. * snd_soc_codec_readable_register: Report if a register is readable.
  1642. *
  1643. * @codec: CODEC to query.
  1644. * @reg: Register to query.
  1645. *
  1646. * Boolean function indicating if a CODEC register is readable.
  1647. */
  1648. int snd_soc_codec_readable_register(struct snd_soc_codec *codec,
  1649. unsigned int reg)
  1650. {
  1651. if (codec->readable_register)
  1652. return codec->readable_register(codec, reg);
  1653. else
  1654. return 1;
  1655. }
  1656. EXPORT_SYMBOL_GPL(snd_soc_codec_readable_register);
  1657. /**
  1658. * snd_soc_codec_writable_register: Report if a register is writable.
  1659. *
  1660. * @codec: CODEC to query.
  1661. * @reg: Register to query.
  1662. *
  1663. * Boolean function indicating if a CODEC register is writable.
  1664. */
  1665. int snd_soc_codec_writable_register(struct snd_soc_codec *codec,
  1666. unsigned int reg)
  1667. {
  1668. if (codec->writable_register)
  1669. return codec->writable_register(codec, reg);
  1670. else
  1671. return 1;
  1672. }
  1673. EXPORT_SYMBOL_GPL(snd_soc_codec_writable_register);
  1674. int snd_soc_platform_read(struct snd_soc_platform *platform,
  1675. unsigned int reg)
  1676. {
  1677. unsigned int ret;
  1678. if (!platform->driver->read) {
  1679. dev_err(platform->dev, "ASoC: platform has no read back\n");
  1680. return -1;
  1681. }
  1682. ret = platform->driver->read(platform, reg);
  1683. dev_dbg(platform->dev, "read %x => %x\n", reg, ret);
  1684. trace_snd_soc_preg_read(platform, reg, ret);
  1685. return ret;
  1686. }
  1687. EXPORT_SYMBOL_GPL(snd_soc_platform_read);
  1688. int snd_soc_platform_write(struct snd_soc_platform *platform,
  1689. unsigned int reg, unsigned int val)
  1690. {
  1691. if (!platform->driver->write) {
  1692. dev_err(platform->dev, "ASoC: platform has no write back\n");
  1693. return -1;
  1694. }
  1695. dev_dbg(platform->dev, "write %x = %x\n", reg, val);
  1696. trace_snd_soc_preg_write(platform, reg, val);
  1697. return platform->driver->write(platform, reg, val);
  1698. }
  1699. EXPORT_SYMBOL_GPL(snd_soc_platform_write);
  1700. /**
  1701. * snd_soc_new_ac97_codec - initailise AC97 device
  1702. * @codec: audio codec
  1703. * @ops: AC97 bus operations
  1704. * @num: AC97 codec number
  1705. *
  1706. * Initialises AC97 codec resources for use by ad-hoc devices only.
  1707. */
  1708. int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
  1709. struct snd_ac97_bus_ops *ops, int num)
  1710. {
  1711. mutex_lock(&codec->mutex);
  1712. codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
  1713. if (codec->ac97 == NULL) {
  1714. mutex_unlock(&codec->mutex);
  1715. return -ENOMEM;
  1716. }
  1717. codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
  1718. if (codec->ac97->bus == NULL) {
  1719. kfree(codec->ac97);
  1720. codec->ac97 = NULL;
  1721. mutex_unlock(&codec->mutex);
  1722. return -ENOMEM;
  1723. }
  1724. codec->ac97->bus->ops = ops;
  1725. codec->ac97->num = num;
  1726. /*
  1727. * Mark the AC97 device to be created by us. This way we ensure that the
  1728. * device will be registered with the device subsystem later on.
  1729. */
  1730. codec->ac97_created = 1;
  1731. mutex_unlock(&codec->mutex);
  1732. return 0;
  1733. }
  1734. EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
  1735. /**
  1736. * snd_soc_free_ac97_codec - free AC97 codec device
  1737. * @codec: audio codec
  1738. *
  1739. * Frees AC97 codec device resources.
  1740. */
  1741. void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
  1742. {
  1743. mutex_lock(&codec->mutex);
  1744. #ifdef CONFIG_SND_SOC_AC97_BUS
  1745. soc_unregister_ac97_dai_link(codec);
  1746. #endif
  1747. kfree(codec->ac97->bus);
  1748. kfree(codec->ac97);
  1749. codec->ac97 = NULL;
  1750. codec->ac97_created = 0;
  1751. mutex_unlock(&codec->mutex);
  1752. }
  1753. EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
  1754. unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
  1755. {
  1756. unsigned int ret;
  1757. ret = codec->read(codec, reg);
  1758. dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
  1759. trace_snd_soc_reg_read(codec, reg, ret);
  1760. return ret;
  1761. }
  1762. EXPORT_SYMBOL_GPL(snd_soc_read);
  1763. unsigned int snd_soc_write(struct snd_soc_codec *codec,
  1764. unsigned int reg, unsigned int val)
  1765. {
  1766. dev_dbg(codec->dev, "write %x = %x\n", reg, val);
  1767. trace_snd_soc_reg_write(codec, reg, val);
  1768. return codec->write(codec, reg, val);
  1769. }
  1770. EXPORT_SYMBOL_GPL(snd_soc_write);
  1771. unsigned int snd_soc_bulk_write_raw(struct snd_soc_codec *codec,
  1772. unsigned int reg, const void *data, size_t len)
  1773. {
  1774. return codec->bulk_write_raw(codec, reg, data, len);
  1775. }
  1776. EXPORT_SYMBOL_GPL(snd_soc_bulk_write_raw);
  1777. /**
  1778. * snd_soc_update_bits - update codec register bits
  1779. * @codec: audio codec
  1780. * @reg: codec register
  1781. * @mask: register mask
  1782. * @value: new value
  1783. *
  1784. * Writes new register value.
  1785. *
  1786. * Returns 1 for change, 0 for no change, or negative error code.
  1787. */
  1788. int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
  1789. unsigned int mask, unsigned int value)
  1790. {
  1791. bool change;
  1792. unsigned int old, new;
  1793. int ret;
  1794. if (codec->using_regmap) {
  1795. ret = regmap_update_bits_check(codec->control_data, reg,
  1796. mask, value, &change);
  1797. } else {
  1798. ret = snd_soc_read(codec, reg);
  1799. if (ret < 0)
  1800. return ret;
  1801. old = ret;
  1802. new = (old & ~mask) | (value & mask);
  1803. change = old != new;
  1804. if (change)
  1805. ret = snd_soc_write(codec, reg, new);
  1806. }
  1807. if (ret < 0)
  1808. return ret;
  1809. return change;
  1810. }
  1811. EXPORT_SYMBOL_GPL(snd_soc_update_bits);
  1812. /**
  1813. * snd_soc_update_bits_locked - update codec register bits
  1814. * @codec: audio codec
  1815. * @reg: codec register
  1816. * @mask: register mask
  1817. * @value: new value
  1818. *
  1819. * Writes new register value, and takes the codec mutex.
  1820. *
  1821. * Returns 1 for change else 0.
  1822. */
  1823. int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
  1824. unsigned short reg, unsigned int mask,
  1825. unsigned int value)
  1826. {
  1827. int change;
  1828. mutex_lock(&codec->mutex);
  1829. change = snd_soc_update_bits(codec, reg, mask, value);
  1830. mutex_unlock(&codec->mutex);
  1831. return change;
  1832. }
  1833. EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
  1834. /**
  1835. * snd_soc_test_bits - test register for change
  1836. * @codec: audio codec
  1837. * @reg: codec register
  1838. * @mask: register mask
  1839. * @value: new value
  1840. *
  1841. * Tests a register with a new value and checks if the new value is
  1842. * different from the old value.
  1843. *
  1844. * Returns 1 for change else 0.
  1845. */
  1846. int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
  1847. unsigned int mask, unsigned int value)
  1848. {
  1849. int change;
  1850. unsigned int old, new;
  1851. old = snd_soc_read(codec, reg);
  1852. new = (old & ~mask) | value;
  1853. change = old != new;
  1854. return change;
  1855. }
  1856. EXPORT_SYMBOL_GPL(snd_soc_test_bits);
  1857. /**
  1858. * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
  1859. * @substream: the pcm substream
  1860. * @hw: the hardware parameters
  1861. *
  1862. * Sets the substream runtime hardware parameters.
  1863. */
  1864. int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
  1865. const struct snd_pcm_hardware *hw)
  1866. {
  1867. struct snd_pcm_runtime *runtime = substream->runtime;
  1868. runtime->hw.info = hw->info;
  1869. runtime->hw.formats = hw->formats;
  1870. runtime->hw.period_bytes_min = hw->period_bytes_min;
  1871. runtime->hw.period_bytes_max = hw->period_bytes_max;
  1872. runtime->hw.periods_min = hw->periods_min;
  1873. runtime->hw.periods_max = hw->periods_max;
  1874. runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
  1875. runtime->hw.fifo_size = hw->fifo_size;
  1876. return 0;
  1877. }
  1878. EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
  1879. /**
  1880. * snd_soc_cnew - create new control
  1881. * @_template: control template
  1882. * @data: control private data
  1883. * @long_name: control long name
  1884. * @prefix: control name prefix
  1885. *
  1886. * Create a new mixer control from a template control.
  1887. *
  1888. * Returns 0 for success, else error.
  1889. */
  1890. struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
  1891. void *data, const char *long_name,
  1892. const char *prefix)
  1893. {
  1894. struct snd_kcontrol_new template;
  1895. struct snd_kcontrol *kcontrol;
  1896. char *name = NULL;
  1897. int name_len;
  1898. memcpy(&template, _template, sizeof(template));
  1899. template.index = 0;
  1900. if (!long_name)
  1901. long_name = template.name;
  1902. if (prefix) {
  1903. name_len = strlen(long_name) + strlen(prefix) + 2;
  1904. name = kmalloc(name_len, GFP_KERNEL);
  1905. if (!name)
  1906. return NULL;
  1907. snprintf(name, name_len, "%s %s", prefix, long_name);
  1908. template.name = name;
  1909. } else {
  1910. template.name = long_name;
  1911. }
  1912. kcontrol = snd_ctl_new1(&template, data);
  1913. kfree(name);
  1914. return kcontrol;
  1915. }
  1916. EXPORT_SYMBOL_GPL(snd_soc_cnew);
  1917. static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
  1918. const struct snd_kcontrol_new *controls, int num_controls,
  1919. const char *prefix, void *data)
  1920. {
  1921. int err, i;
  1922. for (i = 0; i < num_controls; i++) {
  1923. const struct snd_kcontrol_new *control = &controls[i];
  1924. err = snd_ctl_add(card, snd_soc_cnew(control, data,
  1925. control->name, prefix));
  1926. if (err < 0) {
  1927. dev_err(dev, "ASoC: Failed to add %s: %d\n",
  1928. control->name, err);
  1929. return err;
  1930. }
  1931. }
  1932. return 0;
  1933. }
  1934. /**
  1935. * snd_soc_add_codec_controls - add an array of controls to a codec.
  1936. * Convenience function to add a list of controls. Many codecs were
  1937. * duplicating this code.
  1938. *
  1939. * @codec: codec to add controls to
  1940. * @controls: array of controls to add
  1941. * @num_controls: number of elements in the array
  1942. *
  1943. * Return 0 for success, else error.
  1944. */
  1945. int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
  1946. const struct snd_kcontrol_new *controls, int num_controls)
  1947. {
  1948. struct snd_card *card = codec->card->snd_card;
  1949. return snd_soc_add_controls(card, codec->dev, controls, num_controls,
  1950. codec->name_prefix, codec);
  1951. }
  1952. EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
  1953. /**
  1954. * snd_soc_add_platform_controls - add an array of controls to a platform.
  1955. * Convenience function to add a list of controls.
  1956. *
  1957. * @platform: platform to add controls to
  1958. * @controls: array of controls to add
  1959. * @num_controls: number of elements in the array
  1960. *
  1961. * Return 0 for success, else error.
  1962. */
  1963. int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
  1964. const struct snd_kcontrol_new *controls, int num_controls)
  1965. {
  1966. struct snd_card *card = platform->card->snd_card;
  1967. return snd_soc_add_controls(card, platform->dev, controls, num_controls,
  1968. NULL, platform);
  1969. }
  1970. EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
  1971. /**
  1972. * snd_soc_add_card_controls - add an array of controls to a SoC card.
  1973. * Convenience function to add a list of controls.
  1974. *
  1975. * @soc_card: SoC card to add controls to
  1976. * @controls: array of controls to add
  1977. * @num_controls: number of elements in the array
  1978. *
  1979. * Return 0 for success, else error.
  1980. */
  1981. int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
  1982. const struct snd_kcontrol_new *controls, int num_controls)
  1983. {
  1984. struct snd_card *card = soc_card->snd_card;
  1985. return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
  1986. NULL, soc_card);
  1987. }
  1988. EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
  1989. /**
  1990. * snd_soc_add_dai_controls - add an array of controls to a DAI.
  1991. * Convienience function to add a list of controls.
  1992. *
  1993. * @dai: DAI to add controls to
  1994. * @controls: array of controls to add
  1995. * @num_controls: number of elements in the array
  1996. *
  1997. * Return 0 for success, else error.
  1998. */
  1999. int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
  2000. const struct snd_kcontrol_new *controls, int num_controls)
  2001. {
  2002. struct snd_card *card = dai->card->snd_card;
  2003. return snd_soc_add_controls(card, dai->dev, controls, num_controls,
  2004. NULL, dai);
  2005. }
  2006. EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
  2007. /**
  2008. * snd_soc_info_enum_double - enumerated double mixer info callback
  2009. * @kcontrol: mixer control
  2010. * @uinfo: control element information
  2011. *
  2012. * Callback to provide information about a double enumerated
  2013. * mixer control.
  2014. *
  2015. * Returns 0 for success.
  2016. */
  2017. int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
  2018. struct snd_ctl_elem_info *uinfo)
  2019. {
  2020. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  2021. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  2022. uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
  2023. uinfo->value.enumerated.items = e->max;
  2024. if (uinfo->value.enumerated.item > e->max - 1)
  2025. uinfo->value.enumerated.item = e->max - 1;
  2026. strcpy(uinfo->value.enumerated.name,
  2027. e->texts[uinfo->value.enumerated.item]);
  2028. return 0;
  2029. }
  2030. EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
  2031. /**
  2032. * snd_soc_get_enum_double - enumerated double mixer get callback
  2033. * @kcontrol: mixer control
  2034. * @ucontrol: control element information
  2035. *
  2036. * Callback to get the value of a double enumerated mixer.
  2037. *
  2038. * Returns 0 for success.
  2039. */
  2040. int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
  2041. struct snd_ctl_elem_value *ucontrol)
  2042. {
  2043. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2044. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  2045. unsigned int val;
  2046. val = snd_soc_read(codec, e->reg);
  2047. ucontrol->value.enumerated.item[0]
  2048. = (val >> e->shift_l) & e->mask;
  2049. if (e->shift_l != e->shift_r)
  2050. ucontrol->value.enumerated.item[1] =
  2051. (val >> e->shift_r) & e->mask;
  2052. return 0;
  2053. }
  2054. EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
  2055. /**
  2056. * snd_soc_put_enum_double - enumerated double mixer put callback
  2057. * @kcontrol: mixer control
  2058. * @ucontrol: control element information
  2059. *
  2060. * Callback to set the value of a double enumerated mixer.
  2061. *
  2062. * Returns 0 for success.
  2063. */
  2064. int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
  2065. struct snd_ctl_elem_value *ucontrol)
  2066. {
  2067. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2068. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  2069. unsigned int val;
  2070. unsigned int mask;
  2071. if (ucontrol->value.enumerated.item[0] > e->max - 1)
  2072. return -EINVAL;
  2073. val = ucontrol->value.enumerated.item[0] << e->shift_l;
  2074. mask = e->mask << e->shift_l;
  2075. if (e->shift_l != e->shift_r) {
  2076. if (ucontrol->value.enumerated.item[1] > e->max - 1)
  2077. return -EINVAL;
  2078. val |= ucontrol->value.enumerated.item[1] << e->shift_r;
  2079. mask |= e->mask << e->shift_r;
  2080. }
  2081. return snd_soc_update_bits_locked(codec, e->reg, mask, val);
  2082. }
  2083. EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
  2084. /**
  2085. * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
  2086. * @kcontrol: mixer control
  2087. * @ucontrol: control element information
  2088. *
  2089. * Callback to get the value of a double semi enumerated mixer.
  2090. *
  2091. * Semi enumerated mixer: the enumerated items are referred as values. Can be
  2092. * used for handling bitfield coded enumeration for example.
  2093. *
  2094. * Returns 0 for success.
  2095. */
  2096. int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
  2097. struct snd_ctl_elem_value *ucontrol)
  2098. {
  2099. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2100. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  2101. unsigned int reg_val, val, mux;
  2102. reg_val = snd_soc_read(codec, e->reg);
  2103. val = (reg_val >> e->shift_l) & e->mask;
  2104. for (mux = 0; mux < e->max; mux++) {
  2105. if (val == e->values[mux])
  2106. break;
  2107. }
  2108. ucontrol->value.enumerated.item[0] = mux;
  2109. if (e->shift_l != e->shift_r) {
  2110. val = (reg_val >> e->shift_r) & e->mask;
  2111. for (mux = 0; mux < e->max; mux++) {
  2112. if (val == e->values[mux])
  2113. break;
  2114. }
  2115. ucontrol->value.enumerated.item[1] = mux;
  2116. }
  2117. return 0;
  2118. }
  2119. EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
  2120. /**
  2121. * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
  2122. * @kcontrol: mixer control
  2123. * @ucontrol: control element information
  2124. *
  2125. * Callback to set the value of a double semi enumerated mixer.
  2126. *
  2127. * Semi enumerated mixer: the enumerated items are referred as values. Can be
  2128. * used for handling bitfield coded enumeration for example.
  2129. *
  2130. * Returns 0 for success.
  2131. */
  2132. int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
  2133. struct snd_ctl_elem_value *ucontrol)
  2134. {
  2135. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2136. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  2137. unsigned int val;
  2138. unsigned int mask;
  2139. if (ucontrol->value.enumerated.item[0] > e->max - 1)
  2140. return -EINVAL;
  2141. val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
  2142. mask = e->mask << e->shift_l;
  2143. if (e->shift_l != e->shift_r) {
  2144. if (ucontrol->value.enumerated.item[1] > e->max - 1)
  2145. return -EINVAL;
  2146. val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
  2147. mask |= e->mask << e->shift_r;
  2148. }
  2149. return snd_soc_update_bits_locked(codec, e->reg, mask, val);
  2150. }
  2151. EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
  2152. /**
  2153. * snd_soc_info_enum_ext - external enumerated single mixer info callback
  2154. * @kcontrol: mixer control
  2155. * @uinfo: control element information
  2156. *
  2157. * Callback to provide information about an external enumerated
  2158. * single mixer.
  2159. *
  2160. * Returns 0 for success.
  2161. */
  2162. int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
  2163. struct snd_ctl_elem_info *uinfo)
  2164. {
  2165. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  2166. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  2167. uinfo->count = 1;
  2168. uinfo->value.enumerated.items = e->max;
  2169. if (uinfo->value.enumerated.item > e->max - 1)
  2170. uinfo->value.enumerated.item = e->max - 1;
  2171. strcpy(uinfo->value.enumerated.name,
  2172. e->texts[uinfo->value.enumerated.item]);
  2173. return 0;
  2174. }
  2175. EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
  2176. /**
  2177. * snd_soc_info_volsw_ext - external single mixer info callback
  2178. * @kcontrol: mixer control
  2179. * @uinfo: control element information
  2180. *
  2181. * Callback to provide information about a single external mixer control.
  2182. *
  2183. * Returns 0 for success.
  2184. */
  2185. int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
  2186. struct snd_ctl_elem_info *uinfo)
  2187. {
  2188. int max = kcontrol->private_value;
  2189. if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
  2190. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  2191. else
  2192. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2193. uinfo->count = 1;
  2194. uinfo->value.integer.min = 0;
  2195. uinfo->value.integer.max = max;
  2196. return 0;
  2197. }
  2198. EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
  2199. /**
  2200. * snd_soc_info_volsw - single mixer info callback
  2201. * @kcontrol: mixer control
  2202. * @uinfo: control element information
  2203. *
  2204. * Callback to provide information about a single mixer control, or a double
  2205. * mixer control that spans 2 registers.
  2206. *
  2207. * Returns 0 for success.
  2208. */
  2209. int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
  2210. struct snd_ctl_elem_info *uinfo)
  2211. {
  2212. struct soc_mixer_control *mc =
  2213. (struct soc_mixer_control *)kcontrol->private_value;
  2214. int platform_max;
  2215. if (!mc->platform_max)
  2216. mc->platform_max = mc->max;
  2217. platform_max = mc->platform_max;
  2218. if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
  2219. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  2220. else
  2221. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2222. uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
  2223. uinfo->value.integer.min = 0;
  2224. uinfo->value.integer.max = platform_max;
  2225. return 0;
  2226. }
  2227. EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
  2228. /**
  2229. * snd_soc_get_volsw - single mixer get callback
  2230. * @kcontrol: mixer control
  2231. * @ucontrol: control element information
  2232. *
  2233. * Callback to get the value of a single mixer control, or a double mixer
  2234. * control that spans 2 registers.
  2235. *
  2236. * Returns 0 for success.
  2237. */
  2238. int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
  2239. struct snd_ctl_elem_value *ucontrol)
  2240. {
  2241. struct soc_mixer_control *mc =
  2242. (struct soc_mixer_control *)kcontrol->private_value;
  2243. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2244. unsigned int reg = mc->reg;
  2245. unsigned int reg2 = mc->rreg;
  2246. unsigned int shift = mc->shift;
  2247. unsigned int rshift = mc->rshift;
  2248. int max = mc->max;
  2249. unsigned int mask = (1 << fls(max)) - 1;
  2250. unsigned int invert = mc->invert;
  2251. ucontrol->value.integer.value[0] =
  2252. (snd_soc_read(codec, reg) >> shift) & mask;
  2253. if (invert)
  2254. ucontrol->value.integer.value[0] =
  2255. max - ucontrol->value.integer.value[0];
  2256. if (snd_soc_volsw_is_stereo(mc)) {
  2257. if (reg == reg2)
  2258. ucontrol->value.integer.value[1] =
  2259. (snd_soc_read(codec, reg) >> rshift) & mask;
  2260. else
  2261. ucontrol->value.integer.value[1] =
  2262. (snd_soc_read(codec, reg2) >> shift) & mask;
  2263. if (invert)
  2264. ucontrol->value.integer.value[1] =
  2265. max - ucontrol->value.integer.value[1];
  2266. }
  2267. return 0;
  2268. }
  2269. EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
  2270. /**
  2271. * snd_soc_put_volsw - single mixer put callback
  2272. * @kcontrol: mixer control
  2273. * @ucontrol: control element information
  2274. *
  2275. * Callback to set the value of a single mixer control, or a double mixer
  2276. * control that spans 2 registers.
  2277. *
  2278. * Returns 0 for success.
  2279. */
  2280. int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
  2281. struct snd_ctl_elem_value *ucontrol)
  2282. {
  2283. struct soc_mixer_control *mc =
  2284. (struct soc_mixer_control *)kcontrol->private_value;
  2285. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2286. unsigned int reg = mc->reg;
  2287. unsigned int reg2 = mc->rreg;
  2288. unsigned int shift = mc->shift;
  2289. unsigned int rshift = mc->rshift;
  2290. int max = mc->max;
  2291. unsigned int mask = (1 << fls(max)) - 1;
  2292. unsigned int invert = mc->invert;
  2293. int err;
  2294. bool type_2r = 0;
  2295. unsigned int val2 = 0;
  2296. unsigned int val, val_mask;
  2297. val = (ucontrol->value.integer.value[0] & mask);
  2298. if (invert)
  2299. val = max - val;
  2300. val_mask = mask << shift;
  2301. val = val << shift;
  2302. if (snd_soc_volsw_is_stereo(mc)) {
  2303. val2 = (ucontrol->value.integer.value[1] & mask);
  2304. if (invert)
  2305. val2 = max - val2;
  2306. if (reg == reg2) {
  2307. val_mask |= mask << rshift;
  2308. val |= val2 << rshift;
  2309. } else {
  2310. val2 = val2 << shift;
  2311. type_2r = 1;
  2312. }
  2313. }
  2314. err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
  2315. if (err < 0)
  2316. return err;
  2317. if (type_2r)
  2318. err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
  2319. return err;
  2320. }
  2321. EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
  2322. /**
  2323. * snd_soc_get_volsw_sx - single mixer get callback
  2324. * @kcontrol: mixer control
  2325. * @ucontrol: control element information
  2326. *
  2327. * Callback to get the value of a single mixer control, or a double mixer
  2328. * control that spans 2 registers.
  2329. *
  2330. * Returns 0 for success.
  2331. */
  2332. int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
  2333. struct snd_ctl_elem_value *ucontrol)
  2334. {
  2335. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2336. struct soc_mixer_control *mc =
  2337. (struct soc_mixer_control *)kcontrol->private_value;
  2338. unsigned int reg = mc->reg;
  2339. unsigned int reg2 = mc->rreg;
  2340. unsigned int shift = mc->shift;
  2341. unsigned int rshift = mc->rshift;
  2342. int max = mc->max;
  2343. int min = mc->min;
  2344. int mask = (1 << (fls(min + max) - 1)) - 1;
  2345. ucontrol->value.integer.value[0] =
  2346. ((snd_soc_read(codec, reg) >> shift) - min) & mask;
  2347. if (snd_soc_volsw_is_stereo(mc))
  2348. ucontrol->value.integer.value[1] =
  2349. ((snd_soc_read(codec, reg2) >> rshift) - min) & mask;
  2350. return 0;
  2351. }
  2352. EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
  2353. /**
  2354. * snd_soc_put_volsw_sx - double mixer set callback
  2355. * @kcontrol: mixer control
  2356. * @uinfo: control element information
  2357. *
  2358. * Callback to set the value of a double mixer control that spans 2 registers.
  2359. *
  2360. * Returns 0 for success.
  2361. */
  2362. int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
  2363. struct snd_ctl_elem_value *ucontrol)
  2364. {
  2365. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2366. struct soc_mixer_control *mc =
  2367. (struct soc_mixer_control *)kcontrol->private_value;
  2368. unsigned int reg = mc->reg;
  2369. unsigned int reg2 = mc->rreg;
  2370. unsigned int shift = mc->shift;
  2371. unsigned int rshift = mc->rshift;
  2372. int max = mc->max;
  2373. int min = mc->min;
  2374. int mask = (1 << (fls(min + max) - 1)) - 1;
  2375. int err = 0;
  2376. unsigned short val, val_mask, val2 = 0;
  2377. val_mask = mask << shift;
  2378. val = (ucontrol->value.integer.value[0] + min) & mask;
  2379. val = val << shift;
  2380. err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
  2381. if (err < 0)
  2382. return err;
  2383. if (snd_soc_volsw_is_stereo(mc)) {
  2384. val_mask = mask << rshift;
  2385. val2 = (ucontrol->value.integer.value[1] + min) & mask;
  2386. val2 = val2 << rshift;
  2387. if (snd_soc_update_bits_locked(codec, reg2, val_mask, val2))
  2388. return err;
  2389. }
  2390. return 0;
  2391. }
  2392. EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
  2393. /**
  2394. * snd_soc_info_volsw_s8 - signed mixer info callback
  2395. * @kcontrol: mixer control
  2396. * @uinfo: control element information
  2397. *
  2398. * Callback to provide information about a signed mixer control.
  2399. *
  2400. * Returns 0 for success.
  2401. */
  2402. int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
  2403. struct snd_ctl_elem_info *uinfo)
  2404. {
  2405. struct soc_mixer_control *mc =
  2406. (struct soc_mixer_control *)kcontrol->private_value;
  2407. int platform_max;
  2408. int min = mc->min;
  2409. if (!mc->platform_max)
  2410. mc->platform_max = mc->max;
  2411. platform_max = mc->platform_max;
  2412. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2413. uinfo->count = 2;
  2414. uinfo->value.integer.min = 0;
  2415. uinfo->value.integer.max = platform_max - min;
  2416. return 0;
  2417. }
  2418. EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
  2419. /**
  2420. * snd_soc_get_volsw_s8 - signed mixer get callback
  2421. * @kcontrol: mixer control
  2422. * @ucontrol: control element information
  2423. *
  2424. * Callback to get the value of a signed mixer control.
  2425. *
  2426. * Returns 0 for success.
  2427. */
  2428. int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
  2429. struct snd_ctl_elem_value *ucontrol)
  2430. {
  2431. struct soc_mixer_control *mc =
  2432. (struct soc_mixer_control *)kcontrol->private_value;
  2433. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2434. unsigned int reg = mc->reg;
  2435. int min = mc->min;
  2436. int val = snd_soc_read(codec, reg);
  2437. ucontrol->value.integer.value[0] =
  2438. ((signed char)(val & 0xff))-min;
  2439. ucontrol->value.integer.value[1] =
  2440. ((signed char)((val >> 8) & 0xff))-min;
  2441. return 0;
  2442. }
  2443. EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
  2444. /**
  2445. * snd_soc_put_volsw_sgn - signed mixer put callback
  2446. * @kcontrol: mixer control
  2447. * @ucontrol: control element information
  2448. *
  2449. * Callback to set the value of a signed mixer control.
  2450. *
  2451. * Returns 0 for success.
  2452. */
  2453. int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
  2454. struct snd_ctl_elem_value *ucontrol)
  2455. {
  2456. struct soc_mixer_control *mc =
  2457. (struct soc_mixer_control *)kcontrol->private_value;
  2458. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2459. unsigned int reg = mc->reg;
  2460. int min = mc->min;
  2461. unsigned int val;
  2462. val = (ucontrol->value.integer.value[0]+min) & 0xff;
  2463. val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
  2464. return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
  2465. }
  2466. EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
  2467. /**
  2468. * snd_soc_info_volsw_range - single mixer info callback with range.
  2469. * @kcontrol: mixer control
  2470. * @uinfo: control element information
  2471. *
  2472. * Callback to provide information, within a range, about a single
  2473. * mixer control.
  2474. *
  2475. * returns 0 for success.
  2476. */
  2477. int snd_soc_info_volsw_range(struct snd_kcontrol *kcontrol,
  2478. struct snd_ctl_elem_info *uinfo)
  2479. {
  2480. struct soc_mixer_control *mc =
  2481. (struct soc_mixer_control *)kcontrol->private_value;
  2482. int platform_max;
  2483. int min = mc->min;
  2484. if (!mc->platform_max)
  2485. mc->platform_max = mc->max;
  2486. platform_max = mc->platform_max;
  2487. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2488. uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
  2489. uinfo->value.integer.min = 0;
  2490. uinfo->value.integer.max = platform_max - min;
  2491. return 0;
  2492. }
  2493. EXPORT_SYMBOL_GPL(snd_soc_info_volsw_range);
  2494. /**
  2495. * snd_soc_put_volsw_range - single mixer put value callback with range.
  2496. * @kcontrol: mixer control
  2497. * @ucontrol: control element information
  2498. *
  2499. * Callback to set the value, within a range, for a single mixer control.
  2500. *
  2501. * Returns 0 for success.
  2502. */
  2503. int snd_soc_put_volsw_range(struct snd_kcontrol *kcontrol,
  2504. struct snd_ctl_elem_value *ucontrol)
  2505. {
  2506. struct soc_mixer_control *mc =
  2507. (struct soc_mixer_control *)kcontrol->private_value;
  2508. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2509. unsigned int reg = mc->reg;
  2510. unsigned int rreg = mc->rreg;
  2511. unsigned int shift = mc->shift;
  2512. int min = mc->min;
  2513. int max = mc->max;
  2514. unsigned int mask = (1 << fls(max)) - 1;
  2515. unsigned int invert = mc->invert;
  2516. unsigned int val, val_mask;
  2517. int ret;
  2518. val = ((ucontrol->value.integer.value[0] + min) & mask);
  2519. if (invert)
  2520. val = max - val;
  2521. val_mask = mask << shift;
  2522. val = val << shift;
  2523. ret = snd_soc_update_bits_locked(codec, reg, val_mask, val);
  2524. if (ret != 0)
  2525. return ret;
  2526. if (snd_soc_volsw_is_stereo(mc)) {
  2527. val = ((ucontrol->value.integer.value[1] + min) & mask);
  2528. if (invert)
  2529. val = max - val;
  2530. val_mask = mask << shift;
  2531. val = val << shift;
  2532. ret = snd_soc_update_bits_locked(codec, rreg, val_mask, val);
  2533. }
  2534. return ret;
  2535. }
  2536. EXPORT_SYMBOL_GPL(snd_soc_put_volsw_range);
  2537. /**
  2538. * snd_soc_get_volsw_range - single mixer get callback with range
  2539. * @kcontrol: mixer control
  2540. * @ucontrol: control element information
  2541. *
  2542. * Callback to get the value, within a range, of a single mixer control.
  2543. *
  2544. * Returns 0 for success.
  2545. */
  2546. int snd_soc_get_volsw_range(struct snd_kcontrol *kcontrol,
  2547. struct snd_ctl_elem_value *ucontrol)
  2548. {
  2549. struct soc_mixer_control *mc =
  2550. (struct soc_mixer_control *)kcontrol->private_value;
  2551. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2552. unsigned int reg = mc->reg;
  2553. unsigned int rreg = mc->rreg;
  2554. unsigned int shift = mc->shift;
  2555. int min = mc->min;
  2556. int max = mc->max;
  2557. unsigned int mask = (1 << fls(max)) - 1;
  2558. unsigned int invert = mc->invert;
  2559. ucontrol->value.integer.value[0] =
  2560. (snd_soc_read(codec, reg) >> shift) & mask;
  2561. if (invert)
  2562. ucontrol->value.integer.value[0] =
  2563. max - ucontrol->value.integer.value[0];
  2564. ucontrol->value.integer.value[0] =
  2565. ucontrol->value.integer.value[0] - min;
  2566. if (snd_soc_volsw_is_stereo(mc)) {
  2567. ucontrol->value.integer.value[1] =
  2568. (snd_soc_read(codec, rreg) >> shift) & mask;
  2569. if (invert)
  2570. ucontrol->value.integer.value[1] =
  2571. max - ucontrol->value.integer.value[1];
  2572. ucontrol->value.integer.value[1] =
  2573. ucontrol->value.integer.value[1] - min;
  2574. }
  2575. return 0;
  2576. }
  2577. EXPORT_SYMBOL_GPL(snd_soc_get_volsw_range);
  2578. /**
  2579. * snd_soc_limit_volume - Set new limit to an existing volume control.
  2580. *
  2581. * @codec: where to look for the control
  2582. * @name: Name of the control
  2583. * @max: new maximum limit
  2584. *
  2585. * Return 0 for success, else error.
  2586. */
  2587. int snd_soc_limit_volume(struct snd_soc_codec *codec,
  2588. const char *name, int max)
  2589. {
  2590. struct snd_card *card = codec->card->snd_card;
  2591. struct snd_kcontrol *kctl;
  2592. struct soc_mixer_control *mc;
  2593. int found = 0;
  2594. int ret = -EINVAL;
  2595. /* Sanity check for name and max */
  2596. if (unlikely(!name || max <= 0))
  2597. return -EINVAL;
  2598. list_for_each_entry(kctl, &card->controls, list) {
  2599. if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
  2600. found = 1;
  2601. break;
  2602. }
  2603. }
  2604. if (found) {
  2605. mc = (struct soc_mixer_control *)kctl->private_value;
  2606. if (max <= mc->max) {
  2607. mc->platform_max = max;
  2608. ret = 0;
  2609. }
  2610. }
  2611. return ret;
  2612. }
  2613. EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
  2614. int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
  2615. struct snd_ctl_elem_info *uinfo)
  2616. {
  2617. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2618. struct soc_bytes *params = (void *)kcontrol->private_value;
  2619. uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
  2620. uinfo->count = params->num_regs * codec->val_bytes;
  2621. return 0;
  2622. }
  2623. EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
  2624. int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
  2625. struct snd_ctl_elem_value *ucontrol)
  2626. {
  2627. struct soc_bytes *params = (void *)kcontrol->private_value;
  2628. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2629. int ret;
  2630. if (codec->using_regmap)
  2631. ret = regmap_raw_read(codec->control_data, params->base,
  2632. ucontrol->value.bytes.data,
  2633. params->num_regs * codec->val_bytes);
  2634. else
  2635. ret = -EINVAL;
  2636. /* Hide any masked bytes to ensure consistent data reporting */
  2637. if (ret == 0 && params->mask) {
  2638. switch (codec->val_bytes) {
  2639. case 1:
  2640. ucontrol->value.bytes.data[0] &= ~params->mask;
  2641. break;
  2642. case 2:
  2643. ((u16 *)(&ucontrol->value.bytes.data))[0]
  2644. &= ~params->mask;
  2645. break;
  2646. case 4:
  2647. ((u32 *)(&ucontrol->value.bytes.data))[0]
  2648. &= ~params->mask;
  2649. break;
  2650. default:
  2651. return -EINVAL;
  2652. }
  2653. }
  2654. return ret;
  2655. }
  2656. EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
  2657. int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
  2658. struct snd_ctl_elem_value *ucontrol)
  2659. {
  2660. struct soc_bytes *params = (void *)kcontrol->private_value;
  2661. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2662. int ret, len;
  2663. unsigned int val;
  2664. void *data;
  2665. if (!codec->using_regmap)
  2666. return -EINVAL;
  2667. len = params->num_regs * codec->val_bytes;
  2668. data = kmemdup(ucontrol->value.bytes.data, len, GFP_KERNEL | GFP_DMA);
  2669. if (!data)
  2670. return -ENOMEM;
  2671. /*
  2672. * If we've got a mask then we need to preserve the register
  2673. * bits. We shouldn't modify the incoming data so take a
  2674. * copy.
  2675. */
  2676. if (params->mask) {
  2677. ret = regmap_read(codec->control_data, params->base, &val);
  2678. if (ret != 0)
  2679. return ret;
  2680. val &= params->mask;
  2681. switch (codec->val_bytes) {
  2682. case 1:
  2683. ((u8 *)data)[0] &= ~params->mask;
  2684. ((u8 *)data)[0] |= val;
  2685. break;
  2686. case 2:
  2687. ((u16 *)data)[0] &= cpu_to_be16(~params->mask);
  2688. ((u16 *)data)[0] |= cpu_to_be16(val);
  2689. break;
  2690. case 4:
  2691. ((u32 *)data)[0] &= cpu_to_be32(~params->mask);
  2692. ((u32 *)data)[0] |= cpu_to_be32(val);
  2693. break;
  2694. default:
  2695. return -EINVAL;
  2696. }
  2697. }
  2698. ret = regmap_raw_write(codec->control_data, params->base,
  2699. data, len);
  2700. kfree(data);
  2701. return ret;
  2702. }
  2703. EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
  2704. /**
  2705. * snd_soc_info_xr_sx - signed multi register info callback
  2706. * @kcontrol: mreg control
  2707. * @uinfo: control element information
  2708. *
  2709. * Callback to provide information of a control that can
  2710. * span multiple codec registers which together
  2711. * forms a single signed value in a MSB/LSB manner.
  2712. *
  2713. * Returns 0 for success.
  2714. */
  2715. int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol,
  2716. struct snd_ctl_elem_info *uinfo)
  2717. {
  2718. struct soc_mreg_control *mc =
  2719. (struct soc_mreg_control *)kcontrol->private_value;
  2720. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2721. uinfo->count = 1;
  2722. uinfo->value.integer.min = mc->min;
  2723. uinfo->value.integer.max = mc->max;
  2724. return 0;
  2725. }
  2726. EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx);
  2727. /**
  2728. * snd_soc_get_xr_sx - signed multi register get callback
  2729. * @kcontrol: mreg control
  2730. * @ucontrol: control element information
  2731. *
  2732. * Callback to get the value of a control that can span
  2733. * multiple codec registers which together forms a single
  2734. * signed value in a MSB/LSB manner. The control supports
  2735. * specifying total no of bits used to allow for bitfields
  2736. * across the multiple codec registers.
  2737. *
  2738. * Returns 0 for success.
  2739. */
  2740. int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol,
  2741. struct snd_ctl_elem_value *ucontrol)
  2742. {
  2743. struct soc_mreg_control *mc =
  2744. (struct soc_mreg_control *)kcontrol->private_value;
  2745. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2746. unsigned int regbase = mc->regbase;
  2747. unsigned int regcount = mc->regcount;
  2748. unsigned int regwshift = codec->driver->reg_word_size * BITS_PER_BYTE;
  2749. unsigned int regwmask = (1<<regwshift)-1;
  2750. unsigned int invert = mc->invert;
  2751. unsigned long mask = (1UL<<mc->nbits)-1;
  2752. long min = mc->min;
  2753. long max = mc->max;
  2754. long val = 0;
  2755. unsigned long regval;
  2756. unsigned int i;
  2757. for (i = 0; i < regcount; i++) {
  2758. regval = snd_soc_read(codec, regbase+i) & regwmask;
  2759. val |= regval << (regwshift*(regcount-i-1));
  2760. }
  2761. val &= mask;
  2762. if (min < 0 && val > max)
  2763. val |= ~mask;
  2764. if (invert)
  2765. val = max - val;
  2766. ucontrol->value.integer.value[0] = val;
  2767. return 0;
  2768. }
  2769. EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx);
  2770. /**
  2771. * snd_soc_put_xr_sx - signed multi register get callback
  2772. * @kcontrol: mreg control
  2773. * @ucontrol: control element information
  2774. *
  2775. * Callback to set the value of a control that can span
  2776. * multiple codec registers which together forms a single
  2777. * signed value in a MSB/LSB manner. The control supports
  2778. * specifying total no of bits used to allow for bitfields
  2779. * across the multiple codec registers.
  2780. *
  2781. * Returns 0 for success.
  2782. */
  2783. int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol,
  2784. struct snd_ctl_elem_value *ucontrol)
  2785. {
  2786. struct soc_mreg_control *mc =
  2787. (struct soc_mreg_control *)kcontrol->private_value;
  2788. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2789. unsigned int regbase = mc->regbase;
  2790. unsigned int regcount = mc->regcount;
  2791. unsigned int regwshift = codec->driver->reg_word_size * BITS_PER_BYTE;
  2792. unsigned int regwmask = (1<<regwshift)-1;
  2793. unsigned int invert = mc->invert;
  2794. unsigned long mask = (1UL<<mc->nbits)-1;
  2795. long max = mc->max;
  2796. long val = ucontrol->value.integer.value[0];
  2797. unsigned int i, regval, regmask;
  2798. int err;
  2799. if (invert)
  2800. val = max - val;
  2801. val &= mask;
  2802. for (i = 0; i < regcount; i++) {
  2803. regval = (val >> (regwshift*(regcount-i-1))) & regwmask;
  2804. regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask;
  2805. err = snd_soc_update_bits_locked(codec, regbase+i,
  2806. regmask, regval);
  2807. if (err < 0)
  2808. return err;
  2809. }
  2810. return 0;
  2811. }
  2812. EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx);
  2813. /**
  2814. * snd_soc_get_strobe - strobe get callback
  2815. * @kcontrol: mixer control
  2816. * @ucontrol: control element information
  2817. *
  2818. * Callback get the value of a strobe mixer control.
  2819. *
  2820. * Returns 0 for success.
  2821. */
  2822. int snd_soc_get_strobe(struct snd_kcontrol *kcontrol,
  2823. struct snd_ctl_elem_value *ucontrol)
  2824. {
  2825. struct soc_mixer_control *mc =
  2826. (struct soc_mixer_control *)kcontrol->private_value;
  2827. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2828. unsigned int reg = mc->reg;
  2829. unsigned int shift = mc->shift;
  2830. unsigned int mask = 1 << shift;
  2831. unsigned int invert = mc->invert != 0;
  2832. unsigned int val = snd_soc_read(codec, reg) & mask;
  2833. if (shift != 0 && val != 0)
  2834. val = val >> shift;
  2835. ucontrol->value.enumerated.item[0] = val ^ invert;
  2836. return 0;
  2837. }
  2838. EXPORT_SYMBOL_GPL(snd_soc_get_strobe);
  2839. /**
  2840. * snd_soc_put_strobe - strobe put callback
  2841. * @kcontrol: mixer control
  2842. * @ucontrol: control element information
  2843. *
  2844. * Callback strobe a register bit to high then low (or the inverse)
  2845. * in one pass of a single mixer enum control.
  2846. *
  2847. * Returns 1 for success.
  2848. */
  2849. int snd_soc_put_strobe(struct snd_kcontrol *kcontrol,
  2850. struct snd_ctl_elem_value *ucontrol)
  2851. {
  2852. struct soc_mixer_control *mc =
  2853. (struct soc_mixer_control *)kcontrol->private_value;
  2854. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2855. unsigned int reg = mc->reg;
  2856. unsigned int shift = mc->shift;
  2857. unsigned int mask = 1 << shift;
  2858. unsigned int invert = mc->invert != 0;
  2859. unsigned int strobe = ucontrol->value.enumerated.item[0] != 0;
  2860. unsigned int val1 = (strobe ^ invert) ? mask : 0;
  2861. unsigned int val2 = (strobe ^ invert) ? 0 : mask;
  2862. int err;
  2863. err = snd_soc_update_bits_locked(codec, reg, mask, val1);
  2864. if (err < 0)
  2865. return err;
  2866. err = snd_soc_update_bits_locked(codec, reg, mask, val2);
  2867. return err;
  2868. }
  2869. EXPORT_SYMBOL_GPL(snd_soc_put_strobe);
  2870. /**
  2871. * snd_soc_dai_set_sysclk - configure DAI system or master clock.
  2872. * @dai: DAI
  2873. * @clk_id: DAI specific clock ID
  2874. * @freq: new clock frequency in Hz
  2875. * @dir: new clock direction - input/output.
  2876. *
  2877. * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
  2878. */
  2879. int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
  2880. unsigned int freq, int dir)
  2881. {
  2882. if (dai->driver && dai->driver->ops->set_sysclk)
  2883. return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
  2884. else if (dai->codec && dai->codec->driver->set_sysclk)
  2885. return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
  2886. freq, dir);
  2887. else
  2888. return -EINVAL;
  2889. }
  2890. EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
  2891. /**
  2892. * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
  2893. * @codec: CODEC
  2894. * @clk_id: DAI specific clock ID
  2895. * @source: Source for the clock
  2896. * @freq: new clock frequency in Hz
  2897. * @dir: new clock direction - input/output.
  2898. *
  2899. * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
  2900. */
  2901. int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
  2902. int source, unsigned int freq, int dir)
  2903. {
  2904. if (codec->driver->set_sysclk)
  2905. return codec->driver->set_sysclk(codec, clk_id, source,
  2906. freq, dir);
  2907. else
  2908. return -EINVAL;
  2909. }
  2910. EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
  2911. /**
  2912. * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
  2913. * @dai: DAI
  2914. * @div_id: DAI specific clock divider ID
  2915. * @div: new clock divisor.
  2916. *
  2917. * Configures the clock dividers. This is used to derive the best DAI bit and
  2918. * frame clocks from the system or master clock. It's best to set the DAI bit
  2919. * and frame clocks as low as possible to save system power.
  2920. */
  2921. int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
  2922. int div_id, int div)
  2923. {
  2924. if (dai->driver && dai->driver->ops->set_clkdiv)
  2925. return dai->driver->ops->set_clkdiv(dai, div_id, div);
  2926. else
  2927. return -EINVAL;
  2928. }
  2929. EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
  2930. /**
  2931. * snd_soc_dai_set_pll - configure DAI PLL.
  2932. * @dai: DAI
  2933. * @pll_id: DAI specific PLL ID
  2934. * @source: DAI specific source for the PLL
  2935. * @freq_in: PLL input clock frequency in Hz
  2936. * @freq_out: requested PLL output clock frequency in Hz
  2937. *
  2938. * Configures and enables PLL to generate output clock based on input clock.
  2939. */
  2940. int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
  2941. unsigned int freq_in, unsigned int freq_out)
  2942. {
  2943. if (dai->driver && dai->driver->ops->set_pll)
  2944. return dai->driver->ops->set_pll(dai, pll_id, source,
  2945. freq_in, freq_out);
  2946. else if (dai->codec && dai->codec->driver->set_pll)
  2947. return dai->codec->driver->set_pll(dai->codec, pll_id, source,
  2948. freq_in, freq_out);
  2949. else
  2950. return -EINVAL;
  2951. }
  2952. EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
  2953. /*
  2954. * snd_soc_codec_set_pll - configure codec PLL.
  2955. * @codec: CODEC
  2956. * @pll_id: DAI specific PLL ID
  2957. * @source: DAI specific source for the PLL
  2958. * @freq_in: PLL input clock frequency in Hz
  2959. * @freq_out: requested PLL output clock frequency in Hz
  2960. *
  2961. * Configures and enables PLL to generate output clock based on input clock.
  2962. */
  2963. int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
  2964. unsigned int freq_in, unsigned int freq_out)
  2965. {
  2966. if (codec->driver->set_pll)
  2967. return codec->driver->set_pll(codec, pll_id, source,
  2968. freq_in, freq_out);
  2969. else
  2970. return -EINVAL;
  2971. }
  2972. EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
  2973. /**
  2974. * snd_soc_dai_set_fmt - configure DAI hardware audio format.
  2975. * @dai: DAI
  2976. * @fmt: SND_SOC_DAIFMT_ format value.
  2977. *
  2978. * Configures the DAI hardware format and clocking.
  2979. */
  2980. int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  2981. {
  2982. if (dai->driver == NULL)
  2983. return -EINVAL;
  2984. if (dai->driver->ops->set_fmt == NULL)
  2985. return -ENOTSUPP;
  2986. return dai->driver->ops->set_fmt(dai, fmt);
  2987. }
  2988. EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
  2989. /**
  2990. * snd_soc_dai_set_tdm_slot - configure DAI TDM.
  2991. * @dai: DAI
  2992. * @tx_mask: bitmask representing active TX slots.
  2993. * @rx_mask: bitmask representing active RX slots.
  2994. * @slots: Number of slots in use.
  2995. * @slot_width: Width in bits for each slot.
  2996. *
  2997. * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
  2998. * specific.
  2999. */
  3000. int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
  3001. unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
  3002. {
  3003. if (dai->driver && dai->driver->ops->set_tdm_slot)
  3004. return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
  3005. slots, slot_width);
  3006. else
  3007. return -EINVAL;
  3008. }
  3009. EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
  3010. /**
  3011. * snd_soc_dai_set_channel_map - configure DAI audio channel map
  3012. * @dai: DAI
  3013. * @tx_num: how many TX channels
  3014. * @tx_slot: pointer to an array which imply the TX slot number channel
  3015. * 0~num-1 uses
  3016. * @rx_num: how many RX channels
  3017. * @rx_slot: pointer to an array which imply the RX slot number channel
  3018. * 0~num-1 uses
  3019. *
  3020. * configure the relationship between channel number and TDM slot number.
  3021. */
  3022. int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
  3023. unsigned int tx_num, unsigned int *tx_slot,
  3024. unsigned int rx_num, unsigned int *rx_slot)
  3025. {
  3026. if (dai->driver && dai->driver->ops->set_channel_map)
  3027. return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
  3028. rx_num, rx_slot);
  3029. else
  3030. return -EINVAL;
  3031. }
  3032. EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
  3033. /**
  3034. * snd_soc_dai_set_tristate - configure DAI system or master clock.
  3035. * @dai: DAI
  3036. * @tristate: tristate enable
  3037. *
  3038. * Tristates the DAI so that others can use it.
  3039. */
  3040. int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
  3041. {
  3042. if (dai->driver && dai->driver->ops->set_tristate)
  3043. return dai->driver->ops->set_tristate(dai, tristate);
  3044. else
  3045. return -EINVAL;
  3046. }
  3047. EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
  3048. /**
  3049. * snd_soc_dai_digital_mute - configure DAI system or master clock.
  3050. * @dai: DAI
  3051. * @mute: mute enable
  3052. * @direction: stream to mute
  3053. *
  3054. * Mutes the DAI DAC.
  3055. */
  3056. int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
  3057. int direction)
  3058. {
  3059. if (!dai->driver)
  3060. return -ENOTSUPP;
  3061. if (dai->driver->ops->mute_stream)
  3062. return dai->driver->ops->mute_stream(dai, mute, direction);
  3063. else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
  3064. dai->driver->ops->digital_mute)
  3065. return dai->driver->ops->digital_mute(dai, mute);
  3066. else
  3067. return -ENOTSUPP;
  3068. }
  3069. EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
  3070. /**
  3071. * snd_soc_register_card - Register a card with the ASoC core
  3072. *
  3073. * @card: Card to register
  3074. *
  3075. */
  3076. int snd_soc_register_card(struct snd_soc_card *card)
  3077. {
  3078. int i, ret;
  3079. if (!card->name || !card->dev)
  3080. return -EINVAL;
  3081. for (i = 0; i < card->num_links; i++) {
  3082. struct snd_soc_dai_link *link = &card->dai_link[i];
  3083. /*
  3084. * Codec must be specified by 1 of name or OF node,
  3085. * not both or neither.
  3086. */
  3087. if (!!link->codec_name == !!link->codec_of_node) {
  3088. dev_err(card->dev, "ASoC: Neither/both codec"
  3089. " name/of_node are set for %s\n", link->name);
  3090. return -EINVAL;
  3091. }
  3092. /* Codec DAI name must be specified */
  3093. if (!link->codec_dai_name) {
  3094. dev_err(card->dev, "ASoC: codec_dai_name not"
  3095. " set for %s\n", link->name);
  3096. return -EINVAL;
  3097. }
  3098. /*
  3099. * Platform may be specified by either name or OF node, but
  3100. * can be left unspecified, and a dummy platform will be used.
  3101. */
  3102. if (link->platform_name && link->platform_of_node) {
  3103. dev_err(card->dev, "ASoC: Both platform name/of_node"
  3104. " are set for %s\n", link->name);
  3105. return -EINVAL;
  3106. }
  3107. /*
  3108. * CPU device may be specified by either name or OF node, but
  3109. * can be left unspecified, and will be matched based on DAI
  3110. * name alone..
  3111. */
  3112. if (link->cpu_name && link->cpu_of_node) {
  3113. dev_err(card->dev, "ASoC: Neither/both "
  3114. "cpu name/of_node are set for %s\n",link->name);
  3115. return -EINVAL;
  3116. }
  3117. /*
  3118. * At least one of CPU DAI name or CPU device name/node must be
  3119. * specified
  3120. */
  3121. if (!link->cpu_dai_name &&
  3122. !(link->cpu_name || link->cpu_of_node)) {
  3123. dev_err(card->dev, "ASoC: Neither cpu_dai_name nor "
  3124. "cpu_name/of_node are set for %s\n", link->name);
  3125. return -EINVAL;
  3126. }
  3127. }
  3128. dev_set_drvdata(card->dev, card);
  3129. snd_soc_initialize_card_lists(card);
  3130. soc_init_card_debugfs(card);
  3131. card->rtd = devm_kzalloc(card->dev,
  3132. sizeof(struct snd_soc_pcm_runtime) *
  3133. (card->num_links + card->num_aux_devs),
  3134. GFP_KERNEL);
  3135. if (card->rtd == NULL)
  3136. return -ENOMEM;
  3137. card->num_rtd = 0;
  3138. card->rtd_aux = &card->rtd[card->num_links];
  3139. for (i = 0; i < card->num_links; i++)
  3140. card->rtd[i].dai_link = &card->dai_link[i];
  3141. INIT_LIST_HEAD(&card->list);
  3142. INIT_LIST_HEAD(&card->dapm_dirty);
  3143. card->instantiated = 0;
  3144. mutex_init(&card->mutex);
  3145. mutex_init(&card->dapm_mutex);
  3146. ret = snd_soc_instantiate_card(card);
  3147. if (ret != 0)
  3148. soc_cleanup_card_debugfs(card);
  3149. return ret;
  3150. }
  3151. EXPORT_SYMBOL_GPL(snd_soc_register_card);
  3152. /**
  3153. * snd_soc_unregister_card - Unregister a card with the ASoC core
  3154. *
  3155. * @card: Card to unregister
  3156. *
  3157. */
  3158. int snd_soc_unregister_card(struct snd_soc_card *card)
  3159. {
  3160. if (card->instantiated)
  3161. soc_cleanup_card_resources(card);
  3162. dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
  3163. return 0;
  3164. }
  3165. EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
  3166. /*
  3167. * Simplify DAI link configuration by removing ".-1" from device names
  3168. * and sanitizing names.
  3169. */
  3170. static char *fmt_single_name(struct device *dev, int *id)
  3171. {
  3172. char *found, name[NAME_SIZE];
  3173. int id1, id2;
  3174. if (dev_name(dev) == NULL)
  3175. return NULL;
  3176. strlcpy(name, dev_name(dev), NAME_SIZE);
  3177. /* are we a "%s.%d" name (platform and SPI components) */
  3178. found = strstr(name, dev->driver->name);
  3179. if (found) {
  3180. /* get ID */
  3181. if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
  3182. /* discard ID from name if ID == -1 */
  3183. if (*id == -1)
  3184. found[strlen(dev->driver->name)] = '\0';
  3185. }
  3186. } else {
  3187. /* I2C component devices are named "bus-addr" */
  3188. if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
  3189. char tmp[NAME_SIZE];
  3190. /* create unique ID number from I2C addr and bus */
  3191. *id = ((id1 & 0xffff) << 16) + id2;
  3192. /* sanitize component name for DAI link creation */
  3193. snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
  3194. strlcpy(name, tmp, NAME_SIZE);
  3195. } else
  3196. *id = 0;
  3197. }
  3198. return kstrdup(name, GFP_KERNEL);
  3199. }
  3200. /*
  3201. * Simplify DAI link naming for single devices with multiple DAIs by removing
  3202. * any ".-1" and using the DAI name (instead of device name).
  3203. */
  3204. static inline char *fmt_multiple_name(struct device *dev,
  3205. struct snd_soc_dai_driver *dai_drv)
  3206. {
  3207. if (dai_drv->name == NULL) {
  3208. dev_err(dev, "ASoC: error - multiple DAI %s registered with"
  3209. " no name\n", dev_name(dev));
  3210. return NULL;
  3211. }
  3212. return kstrdup(dai_drv->name, GFP_KERNEL);
  3213. }
  3214. /**
  3215. * snd_soc_register_dai - Register a DAI with the ASoC core
  3216. *
  3217. * @dai: DAI to register
  3218. */
  3219. int snd_soc_register_dai(struct device *dev,
  3220. struct snd_soc_dai_driver *dai_drv)
  3221. {
  3222. struct snd_soc_codec *codec;
  3223. struct snd_soc_dai *dai;
  3224. dev_dbg(dev, "ASoC: dai register %s\n", dev_name(dev));
  3225. dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
  3226. if (dai == NULL)
  3227. return -ENOMEM;
  3228. /* create DAI component name */
  3229. dai->name = fmt_single_name(dev, &dai->id);
  3230. if (dai->name == NULL) {
  3231. kfree(dai);
  3232. return -ENOMEM;
  3233. }
  3234. dai->dev = dev;
  3235. dai->driver = dai_drv;
  3236. dai->dapm.dev = dev;
  3237. if (!dai->driver->ops)
  3238. dai->driver->ops = &null_dai_ops;
  3239. mutex_lock(&client_mutex);
  3240. list_for_each_entry(codec, &codec_list, list) {
  3241. if (codec->dev == dev) {
  3242. dev_dbg(dev, "ASoC: Mapped DAI %s to CODEC %s\n",
  3243. dai->name, codec->name);
  3244. dai->codec = codec;
  3245. break;
  3246. }
  3247. }
  3248. if (!dai->codec)
  3249. dai->dapm.idle_bias_off = 1;
  3250. list_add(&dai->list, &dai_list);
  3251. mutex_unlock(&client_mutex);
  3252. dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
  3253. return 0;
  3254. }
  3255. EXPORT_SYMBOL_GPL(snd_soc_register_dai);
  3256. /**
  3257. * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
  3258. *
  3259. * @dai: DAI to unregister
  3260. */
  3261. void snd_soc_unregister_dai(struct device *dev)
  3262. {
  3263. struct snd_soc_dai *dai;
  3264. list_for_each_entry(dai, &dai_list, list) {
  3265. if (dev == dai->dev)
  3266. goto found;
  3267. }
  3268. return;
  3269. found:
  3270. mutex_lock(&client_mutex);
  3271. list_del(&dai->list);
  3272. mutex_unlock(&client_mutex);
  3273. dev_dbg(dev, "ASoC: Unregistered DAI '%s'\n", dai->name);
  3274. kfree(dai->name);
  3275. kfree(dai);
  3276. }
  3277. EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
  3278. /**
  3279. * snd_soc_register_dais - Register multiple DAIs with the ASoC core
  3280. *
  3281. * @dai: Array of DAIs to register
  3282. * @count: Number of DAIs
  3283. */
  3284. int snd_soc_register_dais(struct device *dev,
  3285. struct snd_soc_dai_driver *dai_drv, size_t count)
  3286. {
  3287. struct snd_soc_codec *codec;
  3288. struct snd_soc_dai *dai;
  3289. int i, ret = 0;
  3290. dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count);
  3291. for (i = 0; i < count; i++) {
  3292. dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
  3293. if (dai == NULL) {
  3294. ret = -ENOMEM;
  3295. goto err;
  3296. }
  3297. /* create DAI component name */
  3298. dai->name = fmt_multiple_name(dev, &dai_drv[i]);
  3299. if (dai->name == NULL) {
  3300. kfree(dai);
  3301. ret = -EINVAL;
  3302. goto err;
  3303. }
  3304. dai->dev = dev;
  3305. dai->driver = &dai_drv[i];
  3306. if (dai->driver->id)
  3307. dai->id = dai->driver->id;
  3308. else
  3309. dai->id = i;
  3310. dai->dapm.dev = dev;
  3311. if (!dai->driver->ops)
  3312. dai->driver->ops = &null_dai_ops;
  3313. mutex_lock(&client_mutex);
  3314. list_for_each_entry(codec, &codec_list, list) {
  3315. if (codec->dev == dev) {
  3316. dev_dbg(dev, "ASoC: Mapped DAI %s to "
  3317. "CODEC %s\n", dai->name, codec->name);
  3318. dai->codec = codec;
  3319. break;
  3320. }
  3321. }
  3322. if (!dai->codec)
  3323. dai->dapm.idle_bias_off = 1;
  3324. list_add(&dai->list, &dai_list);
  3325. mutex_unlock(&client_mutex);
  3326. dev_dbg(dai->dev, "ASoC: Registered DAI '%s'\n", dai->name);
  3327. }
  3328. return 0;
  3329. err:
  3330. for (i--; i >= 0; i--)
  3331. snd_soc_unregister_dai(dev);
  3332. return ret;
  3333. }
  3334. EXPORT_SYMBOL_GPL(snd_soc_register_dais);
  3335. /**
  3336. * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
  3337. *
  3338. * @dai: Array of DAIs to unregister
  3339. * @count: Number of DAIs
  3340. */
  3341. void snd_soc_unregister_dais(struct device *dev, size_t count)
  3342. {
  3343. int i;
  3344. for (i = 0; i < count; i++)
  3345. snd_soc_unregister_dai(dev);
  3346. }
  3347. EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
  3348. /**
  3349. * snd_soc_register_platform - Register a platform with the ASoC core
  3350. *
  3351. * @platform: platform to register
  3352. */
  3353. int snd_soc_register_platform(struct device *dev,
  3354. struct snd_soc_platform_driver *platform_drv)
  3355. {
  3356. struct snd_soc_platform *platform;
  3357. dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
  3358. platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
  3359. if (platform == NULL)
  3360. return -ENOMEM;
  3361. /* create platform component name */
  3362. platform->name = fmt_single_name(dev, &platform->id);
  3363. if (platform->name == NULL) {
  3364. kfree(platform);
  3365. return -ENOMEM;
  3366. }
  3367. platform->dev = dev;
  3368. platform->driver = platform_drv;
  3369. platform->dapm.dev = dev;
  3370. platform->dapm.platform = platform;
  3371. platform->dapm.stream_event = platform_drv->stream_event;
  3372. mutex_init(&platform->mutex);
  3373. mutex_lock(&client_mutex);
  3374. list_add(&platform->list, &platform_list);
  3375. mutex_unlock(&client_mutex);
  3376. dev_dbg(dev, "ASoC: Registered platform '%s'\n", platform->name);
  3377. return 0;
  3378. }
  3379. EXPORT_SYMBOL_GPL(snd_soc_register_platform);
  3380. /**
  3381. * snd_soc_unregister_platform - Unregister a platform from the ASoC core
  3382. *
  3383. * @platform: platform to unregister
  3384. */
  3385. void snd_soc_unregister_platform(struct device *dev)
  3386. {
  3387. struct snd_soc_platform *platform;
  3388. list_for_each_entry(platform, &platform_list, list) {
  3389. if (dev == platform->dev)
  3390. goto found;
  3391. }
  3392. return;
  3393. found:
  3394. mutex_lock(&client_mutex);
  3395. list_del(&platform->list);
  3396. mutex_unlock(&client_mutex);
  3397. dev_dbg(dev, "ASoC: Unregistered platform '%s'\n", platform->name);
  3398. kfree(platform->name);
  3399. kfree(platform);
  3400. }
  3401. EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
  3402. static u64 codec_format_map[] = {
  3403. SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
  3404. SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
  3405. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
  3406. SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
  3407. SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
  3408. SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
  3409. SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
  3410. SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
  3411. SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
  3412. SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
  3413. SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
  3414. SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
  3415. SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
  3416. SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
  3417. SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
  3418. | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
  3419. };
  3420. /* Fix up the DAI formats for endianness: codecs don't actually see
  3421. * the endianness of the data but we're using the CPU format
  3422. * definitions which do need to include endianness so we ensure that
  3423. * codec DAIs always have both big and little endian variants set.
  3424. */
  3425. static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
  3426. {
  3427. int i;
  3428. for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
  3429. if (stream->formats & codec_format_map[i])
  3430. stream->formats |= codec_format_map[i];
  3431. }
  3432. /**
  3433. * snd_soc_register_codec - Register a codec with the ASoC core
  3434. *
  3435. * @codec: codec to register
  3436. */
  3437. int snd_soc_register_codec(struct device *dev,
  3438. const struct snd_soc_codec_driver *codec_drv,
  3439. struct snd_soc_dai_driver *dai_drv,
  3440. int num_dai)
  3441. {
  3442. size_t reg_size;
  3443. struct snd_soc_codec *codec;
  3444. int ret, i;
  3445. dev_dbg(dev, "codec register %s\n", dev_name(dev));
  3446. codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
  3447. if (codec == NULL)
  3448. return -ENOMEM;
  3449. /* create CODEC component name */
  3450. codec->name = fmt_single_name(dev, &codec->id);
  3451. if (codec->name == NULL) {
  3452. kfree(codec);
  3453. return -ENOMEM;
  3454. }
  3455. if (codec_drv->compress_type)
  3456. codec->compress_type = codec_drv->compress_type;
  3457. else
  3458. codec->compress_type = SND_SOC_FLAT_COMPRESSION;
  3459. codec->write = codec_drv->write;
  3460. codec->read = codec_drv->read;
  3461. codec->volatile_register = codec_drv->volatile_register;
  3462. codec->readable_register = codec_drv->readable_register;
  3463. codec->writable_register = codec_drv->writable_register;
  3464. codec->ignore_pmdown_time = codec_drv->ignore_pmdown_time;
  3465. codec->dapm.bias_level = SND_SOC_BIAS_OFF;
  3466. codec->dapm.dev = dev;
  3467. codec->dapm.codec = codec;
  3468. codec->dapm.seq_notifier = codec_drv->seq_notifier;
  3469. codec->dapm.stream_event = codec_drv->stream_event;
  3470. codec->dev = dev;
  3471. codec->driver = codec_drv;
  3472. codec->num_dai = num_dai;
  3473. mutex_init(&codec->mutex);
  3474. /* allocate CODEC register cache */
  3475. if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
  3476. reg_size = codec_drv->reg_cache_size * codec_drv->reg_word_size;
  3477. codec->reg_size = reg_size;
  3478. /* it is necessary to make a copy of the default register cache
  3479. * because in the case of using a compression type that requires
  3480. * the default register cache to be marked as the
  3481. * kernel might have freed the array by the time we initialize
  3482. * the cache.
  3483. */
  3484. if (codec_drv->reg_cache_default) {
  3485. codec->reg_def_copy = kmemdup(codec_drv->reg_cache_default,
  3486. reg_size, GFP_KERNEL);
  3487. if (!codec->reg_def_copy) {
  3488. ret = -ENOMEM;
  3489. goto fail;
  3490. }
  3491. }
  3492. }
  3493. if (codec_drv->reg_access_size && codec_drv->reg_access_default) {
  3494. if (!codec->volatile_register)
  3495. codec->volatile_register = snd_soc_default_volatile_register;
  3496. if (!codec->readable_register)
  3497. codec->readable_register = snd_soc_default_readable_register;
  3498. if (!codec->writable_register)
  3499. codec->writable_register = snd_soc_default_writable_register;
  3500. }
  3501. for (i = 0; i < num_dai; i++) {
  3502. fixup_codec_formats(&dai_drv[i].playback);
  3503. fixup_codec_formats(&dai_drv[i].capture);
  3504. }
  3505. mutex_lock(&client_mutex);
  3506. list_add(&codec->list, &codec_list);
  3507. mutex_unlock(&client_mutex);
  3508. /* register any DAIs */
  3509. if (num_dai) {
  3510. ret = snd_soc_register_dais(dev, dai_drv, num_dai);
  3511. if (ret < 0)
  3512. dev_err(codec->dev, "ASoC: Failed to regster"
  3513. " DAIs: %d\n", ret);
  3514. }
  3515. dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n", codec->name);
  3516. return 0;
  3517. fail:
  3518. kfree(codec->name);
  3519. kfree(codec);
  3520. return ret;
  3521. }
  3522. EXPORT_SYMBOL_GPL(snd_soc_register_codec);
  3523. /**
  3524. * snd_soc_unregister_codec - Unregister a codec from the ASoC core
  3525. *
  3526. * @codec: codec to unregister
  3527. */
  3528. void snd_soc_unregister_codec(struct device *dev)
  3529. {
  3530. struct snd_soc_codec *codec;
  3531. int i;
  3532. list_for_each_entry(codec, &codec_list, list) {
  3533. if (dev == codec->dev)
  3534. goto found;
  3535. }
  3536. return;
  3537. found:
  3538. if (codec->num_dai)
  3539. for (i = 0; i < codec->num_dai; i++)
  3540. snd_soc_unregister_dai(dev);
  3541. mutex_lock(&client_mutex);
  3542. list_del(&codec->list);
  3543. mutex_unlock(&client_mutex);
  3544. dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n", codec->name);
  3545. snd_soc_cache_exit(codec);
  3546. kfree(codec->reg_def_copy);
  3547. kfree(codec->name);
  3548. kfree(codec);
  3549. }
  3550. EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
  3551. /* Retrieve a card's name from device tree */
  3552. int snd_soc_of_parse_card_name(struct snd_soc_card *card,
  3553. const char *propname)
  3554. {
  3555. struct device_node *np = card->dev->of_node;
  3556. int ret;
  3557. ret = of_property_read_string_index(np, propname, 0, &card->name);
  3558. /*
  3559. * EINVAL means the property does not exist. This is fine providing
  3560. * card->name was previously set, which is checked later in
  3561. * snd_soc_register_card.
  3562. */
  3563. if (ret < 0 && ret != -EINVAL) {
  3564. dev_err(card->dev,
  3565. "ASoC: Property '%s' could not be read: %d\n",
  3566. propname, ret);
  3567. return ret;
  3568. }
  3569. return 0;
  3570. }
  3571. EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
  3572. int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
  3573. const char *propname)
  3574. {
  3575. struct device_node *np = card->dev->of_node;
  3576. int num_routes;
  3577. struct snd_soc_dapm_route *routes;
  3578. int i, ret;
  3579. num_routes = of_property_count_strings(np, propname);
  3580. if (num_routes < 0 || num_routes & 1) {
  3581. dev_err(card->dev, "ASoC: Property '%s' does not exist or its"
  3582. " length is not even\n", propname);
  3583. return -EINVAL;
  3584. }
  3585. num_routes /= 2;
  3586. if (!num_routes) {
  3587. dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
  3588. propname);
  3589. return -EINVAL;
  3590. }
  3591. routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
  3592. GFP_KERNEL);
  3593. if (!routes) {
  3594. dev_err(card->dev,
  3595. "ASoC: Could not allocate DAPM route table\n");
  3596. return -EINVAL;
  3597. }
  3598. for (i = 0; i < num_routes; i++) {
  3599. ret = of_property_read_string_index(np, propname,
  3600. 2 * i, &routes[i].sink);
  3601. if (ret) {
  3602. dev_err(card->dev,
  3603. "ASoC: Property '%s' index %d could not be read: %d\n",
  3604. propname, 2 * i, ret);
  3605. kfree(routes);
  3606. return -EINVAL;
  3607. }
  3608. ret = of_property_read_string_index(np, propname,
  3609. (2 * i) + 1, &routes[i].source);
  3610. if (ret) {
  3611. dev_err(card->dev,
  3612. "ASoC: Property '%s' index %d could not be read: %d\n",
  3613. propname, (2 * i) + 1, ret);
  3614. kfree(routes);
  3615. return -EINVAL;
  3616. }
  3617. }
  3618. card->num_dapm_routes = num_routes;
  3619. card->dapm_routes = routes;
  3620. return 0;
  3621. }
  3622. EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
  3623. unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
  3624. const char *prefix)
  3625. {
  3626. int ret, i;
  3627. char prop[128];
  3628. unsigned int format = 0;
  3629. int bit, frame;
  3630. const char *str;
  3631. struct {
  3632. char *name;
  3633. unsigned int val;
  3634. } of_fmt_table[] = {
  3635. { "i2s", SND_SOC_DAIFMT_I2S },
  3636. { "right_j", SND_SOC_DAIFMT_RIGHT_J },
  3637. { "left_j", SND_SOC_DAIFMT_LEFT_J },
  3638. { "dsp_a", SND_SOC_DAIFMT_DSP_A },
  3639. { "dsp_b", SND_SOC_DAIFMT_DSP_B },
  3640. { "ac97", SND_SOC_DAIFMT_AC97 },
  3641. { "pdm", SND_SOC_DAIFMT_PDM},
  3642. { "msb", SND_SOC_DAIFMT_MSB },
  3643. { "lsb", SND_SOC_DAIFMT_LSB },
  3644. };
  3645. if (!prefix)
  3646. prefix = "";
  3647. /*
  3648. * check "[prefix]format = xxx"
  3649. * SND_SOC_DAIFMT_FORMAT_MASK area
  3650. */
  3651. snprintf(prop, sizeof(prop), "%sformat", prefix);
  3652. ret = of_property_read_string(np, prop, &str);
  3653. if (ret == 0) {
  3654. for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
  3655. if (strcmp(str, of_fmt_table[i].name) == 0) {
  3656. format |= of_fmt_table[i].val;
  3657. break;
  3658. }
  3659. }
  3660. }
  3661. /*
  3662. * check "[prefix]continuous-clock"
  3663. * SND_SOC_DAIFMT_CLOCK_MASK area
  3664. */
  3665. snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
  3666. if (of_get_property(np, prop, NULL))
  3667. format |= SND_SOC_DAIFMT_CONT;
  3668. else
  3669. format |= SND_SOC_DAIFMT_GATED;
  3670. /*
  3671. * check "[prefix]bitclock-inversion"
  3672. * check "[prefix]frame-inversion"
  3673. * SND_SOC_DAIFMT_INV_MASK area
  3674. */
  3675. snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
  3676. bit = !!of_get_property(np, prop, NULL);
  3677. snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
  3678. frame = !!of_get_property(np, prop, NULL);
  3679. switch ((bit << 4) + frame) {
  3680. case 0x11:
  3681. format |= SND_SOC_DAIFMT_IB_IF;
  3682. break;
  3683. case 0x10:
  3684. format |= SND_SOC_DAIFMT_IB_NF;
  3685. break;
  3686. case 0x01:
  3687. format |= SND_SOC_DAIFMT_NB_IF;
  3688. break;
  3689. default:
  3690. /* SND_SOC_DAIFMT_NB_NF is default */
  3691. break;
  3692. }
  3693. /*
  3694. * check "[prefix]bitclock-master"
  3695. * check "[prefix]frame-master"
  3696. * SND_SOC_DAIFMT_MASTER_MASK area
  3697. */
  3698. snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
  3699. bit = !!of_get_property(np, prop, NULL);
  3700. snprintf(prop, sizeof(prop), "%sframe-master", prefix);
  3701. frame = !!of_get_property(np, prop, NULL);
  3702. switch ((bit << 4) + frame) {
  3703. case 0x11:
  3704. format |= SND_SOC_DAIFMT_CBM_CFM;
  3705. break;
  3706. case 0x10:
  3707. format |= SND_SOC_DAIFMT_CBM_CFS;
  3708. break;
  3709. case 0x01:
  3710. format |= SND_SOC_DAIFMT_CBS_CFM;
  3711. break;
  3712. default:
  3713. format |= SND_SOC_DAIFMT_CBS_CFS;
  3714. break;
  3715. }
  3716. return format;
  3717. }
  3718. EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
  3719. static int __init snd_soc_init(void)
  3720. {
  3721. #ifdef CONFIG_DEBUG_FS
  3722. snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
  3723. if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
  3724. pr_warn("ASoC: Failed to create debugfs directory\n");
  3725. snd_soc_debugfs_root = NULL;
  3726. }
  3727. if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
  3728. &codec_list_fops))
  3729. pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
  3730. if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
  3731. &dai_list_fops))
  3732. pr_warn("ASoC: Failed to create DAI list debugfs file\n");
  3733. if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
  3734. &platform_list_fops))
  3735. pr_warn("ASoC: Failed to create platform list debugfs file\n");
  3736. #endif
  3737. snd_soc_util_init();
  3738. return platform_driver_register(&soc_driver);
  3739. }
  3740. module_init(snd_soc_init);
  3741. static void __exit snd_soc_exit(void)
  3742. {
  3743. snd_soc_util_exit();
  3744. #ifdef CONFIG_DEBUG_FS
  3745. debugfs_remove_recursive(snd_soc_debugfs_root);
  3746. #endif
  3747. platform_driver_unregister(&soc_driver);
  3748. }
  3749. module_exit(snd_soc_exit);
  3750. /* Module information */
  3751. MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
  3752. MODULE_DESCRIPTION("ALSA SoC Core");
  3753. MODULE_LICENSE("GPL");
  3754. MODULE_ALIAS("platform:soc-audio");