soc-core.c 113 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491
  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. static LIST_HEAD(component_list);
  56. /*
  57. * This is a timeout to do a DAPM powerdown after a stream is closed().
  58. * It can be used to eliminate pops between different playback streams, e.g.
  59. * between two audio tracks.
  60. */
  61. static int pmdown_time = 5000;
  62. module_param(pmdown_time, int, 0);
  63. MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
  64. /* returns the minimum number of bytes needed to represent
  65. * a particular given value */
  66. static int min_bytes_needed(unsigned long val)
  67. {
  68. int c = 0;
  69. int i;
  70. for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c)
  71. if (val & (1UL << i))
  72. break;
  73. c = (sizeof val * 8) - c;
  74. if (!c || (c % 8))
  75. c = (c + 8) / 8;
  76. else
  77. c /= 8;
  78. return c;
  79. }
  80. /* fill buf which is 'len' bytes with a formatted
  81. * string of the form 'reg: value\n' */
  82. static int format_register_str(struct snd_soc_codec *codec,
  83. unsigned int reg, char *buf, size_t len)
  84. {
  85. int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
  86. int regsize = codec->driver->reg_word_size * 2;
  87. int ret;
  88. char tmpbuf[len + 1];
  89. char regbuf[regsize + 1];
  90. /* since tmpbuf is allocated on the stack, warn the callers if they
  91. * try to abuse this function */
  92. WARN_ON(len > 63);
  93. /* +2 for ': ' and + 1 for '\n' */
  94. if (wordsize + regsize + 2 + 1 != len)
  95. return -EINVAL;
  96. ret = snd_soc_read(codec, reg);
  97. if (ret < 0) {
  98. memset(regbuf, 'X', regsize);
  99. regbuf[regsize] = '\0';
  100. } else {
  101. snprintf(regbuf, regsize + 1, "%.*x", regsize, ret);
  102. }
  103. /* prepare the buffer */
  104. snprintf(tmpbuf, len + 1, "%.*x: %s\n", wordsize, reg, regbuf);
  105. /* copy it back to the caller without the '\0' */
  106. memcpy(buf, tmpbuf, len);
  107. return 0;
  108. }
  109. /* codec register dump */
  110. static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf,
  111. size_t count, loff_t pos)
  112. {
  113. int i, step = 1;
  114. int wordsize, regsize;
  115. int len;
  116. size_t total = 0;
  117. loff_t p = 0;
  118. wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
  119. regsize = codec->driver->reg_word_size * 2;
  120. len = wordsize + regsize + 2 + 1;
  121. if (!codec->driver->reg_cache_size)
  122. return 0;
  123. if (codec->driver->reg_cache_step)
  124. step = codec->driver->reg_cache_step;
  125. for (i = 0; i < codec->driver->reg_cache_size; i += step) {
  126. if (!snd_soc_codec_readable_register(codec, i))
  127. continue;
  128. if (codec->driver->display_register) {
  129. count += codec->driver->display_register(codec, buf + count,
  130. PAGE_SIZE - count, i);
  131. } else {
  132. /* only support larger than PAGE_SIZE bytes debugfs
  133. * entries for the default case */
  134. if (p >= pos) {
  135. if (total + len >= count - 1)
  136. break;
  137. format_register_str(codec, i, buf + total, len);
  138. total += len;
  139. }
  140. p += len;
  141. }
  142. }
  143. total = min(total, count - 1);
  144. return total;
  145. }
  146. static ssize_t codec_reg_show(struct device *dev,
  147. struct device_attribute *attr, char *buf)
  148. {
  149. struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
  150. return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
  151. }
  152. static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
  153. static ssize_t pmdown_time_show(struct device *dev,
  154. struct device_attribute *attr, char *buf)
  155. {
  156. struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
  157. return sprintf(buf, "%ld\n", rtd->pmdown_time);
  158. }
  159. static ssize_t pmdown_time_set(struct device *dev,
  160. struct device_attribute *attr,
  161. const char *buf, size_t count)
  162. {
  163. struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
  164. int ret;
  165. ret = strict_strtol(buf, 10, &rtd->pmdown_time);
  166. if (ret)
  167. return ret;
  168. return count;
  169. }
  170. static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
  171. #ifdef CONFIG_DEBUG_FS
  172. static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
  173. size_t count, loff_t *ppos)
  174. {
  175. ssize_t ret;
  176. struct snd_soc_codec *codec = file->private_data;
  177. char *buf;
  178. if (*ppos < 0 || !count)
  179. return -EINVAL;
  180. buf = kmalloc(count, GFP_KERNEL);
  181. if (!buf)
  182. return -ENOMEM;
  183. ret = soc_codec_reg_show(codec, buf, count, *ppos);
  184. if (ret >= 0) {
  185. if (copy_to_user(user_buf, buf, ret)) {
  186. kfree(buf);
  187. return -EFAULT;
  188. }
  189. *ppos += ret;
  190. }
  191. kfree(buf);
  192. return ret;
  193. }
  194. static ssize_t codec_reg_write_file(struct file *file,
  195. const char __user *user_buf, size_t count, loff_t *ppos)
  196. {
  197. char buf[32];
  198. size_t buf_size;
  199. char *start = buf;
  200. unsigned long reg, value;
  201. struct snd_soc_codec *codec = file->private_data;
  202. buf_size = min(count, (sizeof(buf)-1));
  203. if (copy_from_user(buf, user_buf, buf_size))
  204. return -EFAULT;
  205. buf[buf_size] = 0;
  206. while (*start == ' ')
  207. start++;
  208. reg = simple_strtoul(start, &start, 16);
  209. while (*start == ' ')
  210. start++;
  211. if (strict_strtoul(start, 16, &value))
  212. return -EINVAL;
  213. /* Userspace has been fiddling around behind the kernel's back */
  214. add_taint(TAINT_USER, LOCKDEP_NOW_UNRELIABLE);
  215. snd_soc_write(codec, reg, value);
  216. return buf_size;
  217. }
  218. static const struct file_operations codec_reg_fops = {
  219. .open = simple_open,
  220. .read = codec_reg_read_file,
  221. .write = codec_reg_write_file,
  222. .llseek = default_llseek,
  223. };
  224. static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
  225. {
  226. struct dentry *debugfs_card_root = codec->card->debugfs_card_root;
  227. codec->debugfs_codec_root = debugfs_create_dir(codec->name,
  228. debugfs_card_root);
  229. if (!codec->debugfs_codec_root) {
  230. dev_warn(codec->dev,
  231. "ASoC: Failed to create codec debugfs directory\n");
  232. return;
  233. }
  234. debugfs_create_bool("cache_sync", 0444, codec->debugfs_codec_root,
  235. &codec->cache_sync);
  236. debugfs_create_bool("cache_only", 0444, codec->debugfs_codec_root,
  237. &codec->cache_only);
  238. codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
  239. codec->debugfs_codec_root,
  240. codec, &codec_reg_fops);
  241. if (!codec->debugfs_reg)
  242. dev_warn(codec->dev,
  243. "ASoC: Failed to create codec register debugfs file\n");
  244. snd_soc_dapm_debugfs_init(&codec->dapm, codec->debugfs_codec_root);
  245. }
  246. static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
  247. {
  248. debugfs_remove_recursive(codec->debugfs_codec_root);
  249. }
  250. static void soc_init_platform_debugfs(struct snd_soc_platform *platform)
  251. {
  252. struct dentry *debugfs_card_root = platform->card->debugfs_card_root;
  253. platform->debugfs_platform_root = debugfs_create_dir(platform->name,
  254. debugfs_card_root);
  255. if (!platform->debugfs_platform_root) {
  256. dev_warn(platform->dev,
  257. "ASoC: Failed to create platform debugfs directory\n");
  258. return;
  259. }
  260. snd_soc_dapm_debugfs_init(&platform->dapm,
  261. platform->debugfs_platform_root);
  262. }
  263. static void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
  264. {
  265. debugfs_remove_recursive(platform->debugfs_platform_root);
  266. }
  267. static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
  268. size_t count, loff_t *ppos)
  269. {
  270. char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  271. ssize_t len, ret = 0;
  272. struct snd_soc_codec *codec;
  273. if (!buf)
  274. return -ENOMEM;
  275. list_for_each_entry(codec, &codec_list, list) {
  276. len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
  277. codec->name);
  278. if (len >= 0)
  279. ret += len;
  280. if (ret > PAGE_SIZE) {
  281. ret = PAGE_SIZE;
  282. break;
  283. }
  284. }
  285. if (ret >= 0)
  286. ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
  287. kfree(buf);
  288. return ret;
  289. }
  290. static const struct file_operations codec_list_fops = {
  291. .read = codec_list_read_file,
  292. .llseek = default_llseek,/* read accesses f_pos */
  293. };
  294. static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
  295. size_t count, loff_t *ppos)
  296. {
  297. char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  298. ssize_t len, ret = 0;
  299. struct snd_soc_dai *dai;
  300. if (!buf)
  301. return -ENOMEM;
  302. list_for_each_entry(dai, &dai_list, list) {
  303. len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", dai->name);
  304. if (len >= 0)
  305. ret += len;
  306. if (ret > PAGE_SIZE) {
  307. ret = PAGE_SIZE;
  308. break;
  309. }
  310. }
  311. ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
  312. kfree(buf);
  313. return ret;
  314. }
  315. static const struct file_operations dai_list_fops = {
  316. .read = dai_list_read_file,
  317. .llseek = default_llseek,/* read accesses f_pos */
  318. };
  319. static ssize_t platform_list_read_file(struct file *file,
  320. char __user *user_buf,
  321. size_t count, loff_t *ppos)
  322. {
  323. char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  324. ssize_t len, ret = 0;
  325. struct snd_soc_platform *platform;
  326. if (!buf)
  327. return -ENOMEM;
  328. list_for_each_entry(platform, &platform_list, list) {
  329. len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
  330. platform->name);
  331. if (len >= 0)
  332. ret += len;
  333. if (ret > PAGE_SIZE) {
  334. ret = PAGE_SIZE;
  335. break;
  336. }
  337. }
  338. ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
  339. kfree(buf);
  340. return ret;
  341. }
  342. static const struct file_operations platform_list_fops = {
  343. .read = platform_list_read_file,
  344. .llseek = default_llseek,/* read accesses f_pos */
  345. };
  346. static void soc_init_card_debugfs(struct snd_soc_card *card)
  347. {
  348. card->debugfs_card_root = debugfs_create_dir(card->name,
  349. snd_soc_debugfs_root);
  350. if (!card->debugfs_card_root) {
  351. dev_warn(card->dev,
  352. "ASoC: Failed to create card debugfs directory\n");
  353. return;
  354. }
  355. card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
  356. card->debugfs_card_root,
  357. &card->pop_time);
  358. if (!card->debugfs_pop_time)
  359. dev_warn(card->dev,
  360. "ASoC: Failed to create pop time debugfs file\n");
  361. }
  362. static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
  363. {
  364. debugfs_remove_recursive(card->debugfs_card_root);
  365. }
  366. #else
  367. static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
  368. {
  369. }
  370. static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
  371. {
  372. }
  373. static inline void soc_init_platform_debugfs(struct snd_soc_platform *platform)
  374. {
  375. }
  376. static inline void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
  377. {
  378. }
  379. static inline void soc_init_card_debugfs(struct snd_soc_card *card)
  380. {
  381. }
  382. static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
  383. {
  384. }
  385. #endif
  386. struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
  387. const char *dai_link, int stream)
  388. {
  389. int i;
  390. for (i = 0; i < card->num_links; i++) {
  391. if (card->rtd[i].dai_link->no_pcm &&
  392. !strcmp(card->rtd[i].dai_link->name, dai_link))
  393. return card->rtd[i].pcm->streams[stream].substream;
  394. }
  395. dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
  396. return NULL;
  397. }
  398. EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
  399. struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
  400. const char *dai_link)
  401. {
  402. int i;
  403. for (i = 0; i < card->num_links; i++) {
  404. if (!strcmp(card->rtd[i].dai_link->name, dai_link))
  405. return &card->rtd[i];
  406. }
  407. dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
  408. return NULL;
  409. }
  410. EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
  411. #ifdef CONFIG_SND_SOC_AC97_BUS
  412. /* unregister ac97 codec */
  413. static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
  414. {
  415. if (codec->ac97->dev.bus)
  416. device_unregister(&codec->ac97->dev);
  417. return 0;
  418. }
  419. /* stop no dev release warning */
  420. static void soc_ac97_device_release(struct device *dev){}
  421. /* register ac97 codec to bus */
  422. static int soc_ac97_dev_register(struct snd_soc_codec *codec)
  423. {
  424. int err;
  425. codec->ac97->dev.bus = &ac97_bus_type;
  426. codec->ac97->dev.parent = codec->card->dev;
  427. codec->ac97->dev.release = soc_ac97_device_release;
  428. dev_set_name(&codec->ac97->dev, "%d-%d:%s",
  429. codec->card->snd_card->number, 0, codec->name);
  430. err = device_register(&codec->ac97->dev);
  431. if (err < 0) {
  432. dev_err(codec->dev, "ASoC: Can't register ac97 bus\n");
  433. codec->ac97->dev.bus = NULL;
  434. return err;
  435. }
  436. return 0;
  437. }
  438. #endif
  439. #ifdef CONFIG_PM_SLEEP
  440. /* powers down audio subsystem for suspend */
  441. int snd_soc_suspend(struct device *dev)
  442. {
  443. struct snd_soc_card *card = dev_get_drvdata(dev);
  444. struct snd_soc_codec *codec;
  445. int i;
  446. /* If the initialization of this soc device failed, there is no codec
  447. * associated with it. Just bail out in this case.
  448. */
  449. if (list_empty(&card->codec_dev_list))
  450. return 0;
  451. /* Due to the resume being scheduled into a workqueue we could
  452. * suspend before that's finished - wait for it to complete.
  453. */
  454. snd_power_lock(card->snd_card);
  455. snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
  456. snd_power_unlock(card->snd_card);
  457. /* we're going to block userspace touching us until resume completes */
  458. snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
  459. /* mute any active DACs */
  460. for (i = 0; i < card->num_rtd; i++) {
  461. struct snd_soc_dai *dai = card->rtd[i].codec_dai;
  462. struct snd_soc_dai_driver *drv = dai->driver;
  463. if (card->rtd[i].dai_link->ignore_suspend)
  464. continue;
  465. if (drv->ops->digital_mute && dai->playback_active)
  466. drv->ops->digital_mute(dai, 1);
  467. }
  468. /* suspend all pcms */
  469. for (i = 0; i < card->num_rtd; i++) {
  470. if (card->rtd[i].dai_link->ignore_suspend)
  471. continue;
  472. snd_pcm_suspend_all(card->rtd[i].pcm);
  473. }
  474. if (card->suspend_pre)
  475. card->suspend_pre(card);
  476. for (i = 0; i < card->num_rtd; i++) {
  477. struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
  478. struct snd_soc_platform *platform = card->rtd[i].platform;
  479. if (card->rtd[i].dai_link->ignore_suspend)
  480. continue;
  481. if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
  482. cpu_dai->driver->suspend(cpu_dai);
  483. if (platform->driver->suspend && !platform->suspended) {
  484. platform->driver->suspend(cpu_dai);
  485. platform->suspended = 1;
  486. }
  487. }
  488. /* close any waiting streams and save state */
  489. for (i = 0; i < card->num_rtd; i++) {
  490. flush_delayed_work(&card->rtd[i].delayed_work);
  491. card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level;
  492. }
  493. for (i = 0; i < card->num_rtd; i++) {
  494. if (card->rtd[i].dai_link->ignore_suspend)
  495. continue;
  496. snd_soc_dapm_stream_event(&card->rtd[i],
  497. SNDRV_PCM_STREAM_PLAYBACK,
  498. SND_SOC_DAPM_STREAM_SUSPEND);
  499. snd_soc_dapm_stream_event(&card->rtd[i],
  500. SNDRV_PCM_STREAM_CAPTURE,
  501. SND_SOC_DAPM_STREAM_SUSPEND);
  502. }
  503. /* Recheck all analogue paths too */
  504. dapm_mark_io_dirty(&card->dapm);
  505. snd_soc_dapm_sync(&card->dapm);
  506. /* suspend all CODECs */
  507. list_for_each_entry(codec, &card->codec_dev_list, card_list) {
  508. /* If there are paths active then the CODEC will be held with
  509. * bias _ON and should not be suspended. */
  510. if (!codec->suspended && codec->driver->suspend) {
  511. switch (codec->dapm.bias_level) {
  512. case SND_SOC_BIAS_STANDBY:
  513. /*
  514. * If the CODEC is capable of idle
  515. * bias off then being in STANDBY
  516. * means it's doing something,
  517. * otherwise fall through.
  518. */
  519. if (codec->dapm.idle_bias_off) {
  520. dev_dbg(codec->dev,
  521. "ASoC: idle_bias_off CODEC on 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,
  533. "ASoC: CODEC is on 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,
  589. "ASoC: CODEC was on over 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 with idle_bias_off==1\n",
  917. 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,
  1314. "ASoC: Failed to set cache compression type: %d\n",
  1315. ret);
  1316. return ret;
  1317. }
  1318. codec->cache_init = 1;
  1319. return 0;
  1320. }
  1321. static int snd_soc_instantiate_card(struct snd_soc_card *card)
  1322. {
  1323. struct snd_soc_codec *codec;
  1324. struct snd_soc_codec_conf *codec_conf;
  1325. enum snd_soc_compress_type compress_type;
  1326. struct snd_soc_dai_link *dai_link;
  1327. int ret, i, order, dai_fmt;
  1328. mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
  1329. /* bind DAIs */
  1330. for (i = 0; i < card->num_links; i++) {
  1331. ret = soc_bind_dai_link(card, i);
  1332. if (ret != 0)
  1333. goto base_error;
  1334. }
  1335. /* check aux_devs too */
  1336. for (i = 0; i < card->num_aux_devs; i++) {
  1337. ret = soc_check_aux_dev(card, i);
  1338. if (ret != 0)
  1339. goto base_error;
  1340. }
  1341. /* initialize the register cache for each available codec */
  1342. list_for_each_entry(codec, &codec_list, list) {
  1343. if (codec->cache_init)
  1344. continue;
  1345. /* by default we don't override the compress_type */
  1346. compress_type = 0;
  1347. /* check to see if we need to override the compress_type */
  1348. for (i = 0; i < card->num_configs; ++i) {
  1349. codec_conf = &card->codec_conf[i];
  1350. if (!strcmp(codec->name, codec_conf->dev_name)) {
  1351. compress_type = codec_conf->compress_type;
  1352. if (compress_type && compress_type
  1353. != codec->compress_type)
  1354. break;
  1355. }
  1356. }
  1357. ret = snd_soc_init_codec_cache(codec, compress_type);
  1358. if (ret < 0)
  1359. goto base_error;
  1360. }
  1361. /* card bind complete so register a sound card */
  1362. ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
  1363. card->owner, 0, &card->snd_card);
  1364. if (ret < 0) {
  1365. dev_err(card->dev,
  1366. "ASoC: can't create sound card for card %s: %d\n",
  1367. card->name, ret);
  1368. goto base_error;
  1369. }
  1370. card->snd_card->dev = card->dev;
  1371. card->dapm.bias_level = SND_SOC_BIAS_OFF;
  1372. card->dapm.dev = card->dev;
  1373. card->dapm.card = card;
  1374. list_add(&card->dapm.list, &card->dapm_list);
  1375. #ifdef CONFIG_DEBUG_FS
  1376. snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
  1377. #endif
  1378. #ifdef CONFIG_PM_SLEEP
  1379. /* deferred resume work */
  1380. INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
  1381. #endif
  1382. if (card->dapm_widgets)
  1383. snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
  1384. card->num_dapm_widgets);
  1385. /* initialise the sound card only once */
  1386. if (card->probe) {
  1387. ret = card->probe(card);
  1388. if (ret < 0)
  1389. goto card_probe_error;
  1390. }
  1391. /* probe all components used by DAI links on this card */
  1392. for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
  1393. order++) {
  1394. for (i = 0; i < card->num_links; i++) {
  1395. ret = soc_probe_link_components(card, i, order);
  1396. if (ret < 0) {
  1397. dev_err(card->dev,
  1398. "ASoC: failed to instantiate card %d\n",
  1399. ret);
  1400. goto probe_dai_err;
  1401. }
  1402. }
  1403. }
  1404. /* probe all DAI links on this card */
  1405. for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
  1406. order++) {
  1407. for (i = 0; i < card->num_links; i++) {
  1408. ret = soc_probe_link_dais(card, i, order);
  1409. if (ret < 0) {
  1410. dev_err(card->dev,
  1411. "ASoC: failed to instantiate card %d\n",
  1412. ret);
  1413. goto probe_dai_err;
  1414. }
  1415. }
  1416. }
  1417. for (i = 0; i < card->num_aux_devs; i++) {
  1418. ret = soc_probe_aux_dev(card, i);
  1419. if (ret < 0) {
  1420. dev_err(card->dev,
  1421. "ASoC: failed to add auxiliary devices %d\n",
  1422. ret);
  1423. goto probe_aux_dev_err;
  1424. }
  1425. }
  1426. snd_soc_dapm_link_dai_widgets(card);
  1427. if (card->controls)
  1428. snd_soc_add_card_controls(card, card->controls, card->num_controls);
  1429. if (card->dapm_routes)
  1430. snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
  1431. card->num_dapm_routes);
  1432. snd_soc_dapm_new_widgets(&card->dapm);
  1433. for (i = 0; i < card->num_links; i++) {
  1434. dai_link = &card->dai_link[i];
  1435. dai_fmt = dai_link->dai_fmt;
  1436. if (dai_fmt) {
  1437. ret = snd_soc_dai_set_fmt(card->rtd[i].codec_dai,
  1438. dai_fmt);
  1439. if (ret != 0 && ret != -ENOTSUPP)
  1440. dev_warn(card->rtd[i].codec_dai->dev,
  1441. "ASoC: Failed to set DAI format: %d\n",
  1442. ret);
  1443. }
  1444. /* If this is a regular CPU link there will be a platform */
  1445. if (dai_fmt &&
  1446. (dai_link->platform_name || dai_link->platform_of_node)) {
  1447. ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
  1448. dai_fmt);
  1449. if (ret != 0 && ret != -ENOTSUPP)
  1450. dev_warn(card->rtd[i].cpu_dai->dev,
  1451. "ASoC: Failed to set DAI format: %d\n",
  1452. ret);
  1453. } else if (dai_fmt) {
  1454. /* Flip the polarity for the "CPU" end */
  1455. dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
  1456. switch (dai_link->dai_fmt &
  1457. SND_SOC_DAIFMT_MASTER_MASK) {
  1458. case SND_SOC_DAIFMT_CBM_CFM:
  1459. dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
  1460. break;
  1461. case SND_SOC_DAIFMT_CBM_CFS:
  1462. dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
  1463. break;
  1464. case SND_SOC_DAIFMT_CBS_CFM:
  1465. dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
  1466. break;
  1467. case SND_SOC_DAIFMT_CBS_CFS:
  1468. dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
  1469. break;
  1470. }
  1471. ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
  1472. dai_fmt);
  1473. if (ret != 0 && ret != -ENOTSUPP)
  1474. dev_warn(card->rtd[i].cpu_dai->dev,
  1475. "ASoC: Failed to set DAI format: %d\n",
  1476. ret);
  1477. }
  1478. }
  1479. snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
  1480. "%s", card->name);
  1481. snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
  1482. "%s", card->long_name ? card->long_name : card->name);
  1483. snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
  1484. "%s", card->driver_name ? card->driver_name : card->name);
  1485. for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
  1486. switch (card->snd_card->driver[i]) {
  1487. case '_':
  1488. case '-':
  1489. case '\0':
  1490. break;
  1491. default:
  1492. if (!isalnum(card->snd_card->driver[i]))
  1493. card->snd_card->driver[i] = '_';
  1494. break;
  1495. }
  1496. }
  1497. if (card->late_probe) {
  1498. ret = card->late_probe(card);
  1499. if (ret < 0) {
  1500. dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
  1501. card->name, ret);
  1502. goto probe_aux_dev_err;
  1503. }
  1504. }
  1505. snd_soc_dapm_new_widgets(&card->dapm);
  1506. if (card->fully_routed)
  1507. list_for_each_entry(codec, &card->codec_dev_list, card_list)
  1508. snd_soc_dapm_auto_nc_codec_pins(codec);
  1509. ret = snd_card_register(card->snd_card);
  1510. if (ret < 0) {
  1511. dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
  1512. ret);
  1513. goto probe_aux_dev_err;
  1514. }
  1515. #ifdef CONFIG_SND_SOC_AC97_BUS
  1516. /* register any AC97 codecs */
  1517. for (i = 0; i < card->num_rtd; i++) {
  1518. ret = soc_register_ac97_dai_link(&card->rtd[i]);
  1519. if (ret < 0) {
  1520. dev_err(card->dev,
  1521. "ASoC: failed to register AC97: %d\n", ret);
  1522. while (--i >= 0)
  1523. soc_unregister_ac97_dai_link(card->rtd[i].codec);
  1524. goto probe_aux_dev_err;
  1525. }
  1526. }
  1527. #endif
  1528. card->instantiated = 1;
  1529. snd_soc_dapm_sync(&card->dapm);
  1530. mutex_unlock(&card->mutex);
  1531. return 0;
  1532. probe_aux_dev_err:
  1533. for (i = 0; i < card->num_aux_devs; i++)
  1534. soc_remove_aux_dev(card, i);
  1535. probe_dai_err:
  1536. soc_remove_dai_links(card);
  1537. card_probe_error:
  1538. if (card->remove)
  1539. card->remove(card);
  1540. snd_card_free(card->snd_card);
  1541. base_error:
  1542. mutex_unlock(&card->mutex);
  1543. return ret;
  1544. }
  1545. /* probes a new socdev */
  1546. static int soc_probe(struct platform_device *pdev)
  1547. {
  1548. struct snd_soc_card *card = platform_get_drvdata(pdev);
  1549. /*
  1550. * no card, so machine driver should be registering card
  1551. * we should not be here in that case so ret error
  1552. */
  1553. if (!card)
  1554. return -EINVAL;
  1555. dev_warn(&pdev->dev,
  1556. "ASoC: machine %s should use snd_soc_register_card()\n",
  1557. card->name);
  1558. /* Bodge while we unpick instantiation */
  1559. card->dev = &pdev->dev;
  1560. return snd_soc_register_card(card);
  1561. }
  1562. static int soc_cleanup_card_resources(struct snd_soc_card *card)
  1563. {
  1564. int i;
  1565. /* make sure any delayed work runs */
  1566. for (i = 0; i < card->num_rtd; i++) {
  1567. struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
  1568. flush_delayed_work(&rtd->delayed_work);
  1569. }
  1570. /* remove auxiliary devices */
  1571. for (i = 0; i < card->num_aux_devs; i++)
  1572. soc_remove_aux_dev(card, i);
  1573. /* remove and free each DAI */
  1574. soc_remove_dai_links(card);
  1575. soc_cleanup_card_debugfs(card);
  1576. /* remove the card */
  1577. if (card->remove)
  1578. card->remove(card);
  1579. snd_soc_dapm_free(&card->dapm);
  1580. snd_card_free(card->snd_card);
  1581. return 0;
  1582. }
  1583. /* removes a socdev */
  1584. static int soc_remove(struct platform_device *pdev)
  1585. {
  1586. struct snd_soc_card *card = platform_get_drvdata(pdev);
  1587. snd_soc_unregister_card(card);
  1588. return 0;
  1589. }
  1590. int snd_soc_poweroff(struct device *dev)
  1591. {
  1592. struct snd_soc_card *card = dev_get_drvdata(dev);
  1593. int i;
  1594. if (!card->instantiated)
  1595. return 0;
  1596. /* Flush out pmdown_time work - we actually do want to run it
  1597. * now, we're shutting down so no imminent restart. */
  1598. for (i = 0; i < card->num_rtd; i++) {
  1599. struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
  1600. flush_delayed_work(&rtd->delayed_work);
  1601. }
  1602. snd_soc_dapm_shutdown(card);
  1603. return 0;
  1604. }
  1605. EXPORT_SYMBOL_GPL(snd_soc_poweroff);
  1606. const struct dev_pm_ops snd_soc_pm_ops = {
  1607. .suspend = snd_soc_suspend,
  1608. .resume = snd_soc_resume,
  1609. .freeze = snd_soc_suspend,
  1610. .thaw = snd_soc_resume,
  1611. .poweroff = snd_soc_poweroff,
  1612. .restore = snd_soc_resume,
  1613. };
  1614. EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
  1615. /* ASoC platform driver */
  1616. static struct platform_driver soc_driver = {
  1617. .driver = {
  1618. .name = "soc-audio",
  1619. .owner = THIS_MODULE,
  1620. .pm = &snd_soc_pm_ops,
  1621. },
  1622. .probe = soc_probe,
  1623. .remove = soc_remove,
  1624. };
  1625. /**
  1626. * snd_soc_codec_volatile_register: Report if a register is volatile.
  1627. *
  1628. * @codec: CODEC to query.
  1629. * @reg: Register to query.
  1630. *
  1631. * Boolean function indiciating if a CODEC register is volatile.
  1632. */
  1633. int snd_soc_codec_volatile_register(struct snd_soc_codec *codec,
  1634. unsigned int reg)
  1635. {
  1636. if (codec->volatile_register)
  1637. return codec->volatile_register(codec, reg);
  1638. else
  1639. return 0;
  1640. }
  1641. EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
  1642. /**
  1643. * snd_soc_codec_readable_register: Report if a register is readable.
  1644. *
  1645. * @codec: CODEC to query.
  1646. * @reg: Register to query.
  1647. *
  1648. * Boolean function indicating if a CODEC register is readable.
  1649. */
  1650. int snd_soc_codec_readable_register(struct snd_soc_codec *codec,
  1651. unsigned int reg)
  1652. {
  1653. if (codec->readable_register)
  1654. return codec->readable_register(codec, reg);
  1655. else
  1656. return 1;
  1657. }
  1658. EXPORT_SYMBOL_GPL(snd_soc_codec_readable_register);
  1659. /**
  1660. * snd_soc_codec_writable_register: Report if a register is writable.
  1661. *
  1662. * @codec: CODEC to query.
  1663. * @reg: Register to query.
  1664. *
  1665. * Boolean function indicating if a CODEC register is writable.
  1666. */
  1667. int snd_soc_codec_writable_register(struct snd_soc_codec *codec,
  1668. unsigned int reg)
  1669. {
  1670. if (codec->writable_register)
  1671. return codec->writable_register(codec, reg);
  1672. else
  1673. return 1;
  1674. }
  1675. EXPORT_SYMBOL_GPL(snd_soc_codec_writable_register);
  1676. int snd_soc_platform_read(struct snd_soc_platform *platform,
  1677. unsigned int reg)
  1678. {
  1679. unsigned int ret;
  1680. if (!platform->driver->read) {
  1681. dev_err(platform->dev, "ASoC: platform has no read back\n");
  1682. return -1;
  1683. }
  1684. ret = platform->driver->read(platform, reg);
  1685. dev_dbg(platform->dev, "read %x => %x\n", reg, ret);
  1686. trace_snd_soc_preg_read(platform, reg, ret);
  1687. return ret;
  1688. }
  1689. EXPORT_SYMBOL_GPL(snd_soc_platform_read);
  1690. int snd_soc_platform_write(struct snd_soc_platform *platform,
  1691. unsigned int reg, unsigned int val)
  1692. {
  1693. if (!platform->driver->write) {
  1694. dev_err(platform->dev, "ASoC: platform has no write back\n");
  1695. return -1;
  1696. }
  1697. dev_dbg(platform->dev, "write %x = %x\n", reg, val);
  1698. trace_snd_soc_preg_write(platform, reg, val);
  1699. return platform->driver->write(platform, reg, val);
  1700. }
  1701. EXPORT_SYMBOL_GPL(snd_soc_platform_write);
  1702. /**
  1703. * snd_soc_new_ac97_codec - initailise AC97 device
  1704. * @codec: audio codec
  1705. * @ops: AC97 bus operations
  1706. * @num: AC97 codec number
  1707. *
  1708. * Initialises AC97 codec resources for use by ad-hoc devices only.
  1709. */
  1710. int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
  1711. struct snd_ac97_bus_ops *ops, int num)
  1712. {
  1713. mutex_lock(&codec->mutex);
  1714. codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
  1715. if (codec->ac97 == NULL) {
  1716. mutex_unlock(&codec->mutex);
  1717. return -ENOMEM;
  1718. }
  1719. codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
  1720. if (codec->ac97->bus == NULL) {
  1721. kfree(codec->ac97);
  1722. codec->ac97 = NULL;
  1723. mutex_unlock(&codec->mutex);
  1724. return -ENOMEM;
  1725. }
  1726. codec->ac97->bus->ops = ops;
  1727. codec->ac97->num = num;
  1728. /*
  1729. * Mark the AC97 device to be created by us. This way we ensure that the
  1730. * device will be registered with the device subsystem later on.
  1731. */
  1732. codec->ac97_created = 1;
  1733. mutex_unlock(&codec->mutex);
  1734. return 0;
  1735. }
  1736. EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
  1737. struct snd_ac97_bus_ops *soc_ac97_ops;
  1738. EXPORT_SYMBOL_GPL(soc_ac97_ops);
  1739. int snd_soc_set_ac97_ops(struct snd_ac97_bus_ops *ops)
  1740. {
  1741. if (ops == soc_ac97_ops)
  1742. return 0;
  1743. if (soc_ac97_ops && ops)
  1744. return -EBUSY;
  1745. soc_ac97_ops = ops;
  1746. return 0;
  1747. }
  1748. EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops);
  1749. /**
  1750. * snd_soc_free_ac97_codec - free AC97 codec device
  1751. * @codec: audio codec
  1752. *
  1753. * Frees AC97 codec device resources.
  1754. */
  1755. void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
  1756. {
  1757. mutex_lock(&codec->mutex);
  1758. #ifdef CONFIG_SND_SOC_AC97_BUS
  1759. soc_unregister_ac97_dai_link(codec);
  1760. #endif
  1761. kfree(codec->ac97->bus);
  1762. kfree(codec->ac97);
  1763. codec->ac97 = NULL;
  1764. codec->ac97_created = 0;
  1765. mutex_unlock(&codec->mutex);
  1766. }
  1767. EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
  1768. unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
  1769. {
  1770. unsigned int ret;
  1771. ret = codec->read(codec, reg);
  1772. dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
  1773. trace_snd_soc_reg_read(codec, reg, ret);
  1774. return ret;
  1775. }
  1776. EXPORT_SYMBOL_GPL(snd_soc_read);
  1777. unsigned int snd_soc_write(struct snd_soc_codec *codec,
  1778. unsigned int reg, unsigned int val)
  1779. {
  1780. dev_dbg(codec->dev, "write %x = %x\n", reg, val);
  1781. trace_snd_soc_reg_write(codec, reg, val);
  1782. return codec->write(codec, reg, val);
  1783. }
  1784. EXPORT_SYMBOL_GPL(snd_soc_write);
  1785. unsigned int snd_soc_bulk_write_raw(struct snd_soc_codec *codec,
  1786. unsigned int reg, const void *data, size_t len)
  1787. {
  1788. return codec->bulk_write_raw(codec, reg, data, len);
  1789. }
  1790. EXPORT_SYMBOL_GPL(snd_soc_bulk_write_raw);
  1791. /**
  1792. * snd_soc_update_bits - update codec register bits
  1793. * @codec: audio codec
  1794. * @reg: codec register
  1795. * @mask: register mask
  1796. * @value: new value
  1797. *
  1798. * Writes new register value.
  1799. *
  1800. * Returns 1 for change, 0 for no change, or negative error code.
  1801. */
  1802. int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
  1803. unsigned int mask, unsigned int value)
  1804. {
  1805. bool change;
  1806. unsigned int old, new;
  1807. int ret;
  1808. if (codec->using_regmap) {
  1809. ret = regmap_update_bits_check(codec->control_data, reg,
  1810. mask, value, &change);
  1811. } else {
  1812. ret = snd_soc_read(codec, reg);
  1813. if (ret < 0)
  1814. return ret;
  1815. old = ret;
  1816. new = (old & ~mask) | (value & mask);
  1817. change = old != new;
  1818. if (change)
  1819. ret = snd_soc_write(codec, reg, new);
  1820. }
  1821. if (ret < 0)
  1822. return ret;
  1823. return change;
  1824. }
  1825. EXPORT_SYMBOL_GPL(snd_soc_update_bits);
  1826. /**
  1827. * snd_soc_update_bits_locked - update codec register bits
  1828. * @codec: audio codec
  1829. * @reg: codec register
  1830. * @mask: register mask
  1831. * @value: new value
  1832. *
  1833. * Writes new register value, and takes the codec mutex.
  1834. *
  1835. * Returns 1 for change else 0.
  1836. */
  1837. int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
  1838. unsigned short reg, unsigned int mask,
  1839. unsigned int value)
  1840. {
  1841. int change;
  1842. mutex_lock(&codec->mutex);
  1843. change = snd_soc_update_bits(codec, reg, mask, value);
  1844. mutex_unlock(&codec->mutex);
  1845. return change;
  1846. }
  1847. EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
  1848. /**
  1849. * snd_soc_test_bits - test register for change
  1850. * @codec: audio codec
  1851. * @reg: codec register
  1852. * @mask: register mask
  1853. * @value: new value
  1854. *
  1855. * Tests a register with a new value and checks if the new value is
  1856. * different from the old value.
  1857. *
  1858. * Returns 1 for change else 0.
  1859. */
  1860. int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
  1861. unsigned int mask, unsigned int value)
  1862. {
  1863. int change;
  1864. unsigned int old, new;
  1865. old = snd_soc_read(codec, reg);
  1866. new = (old & ~mask) | value;
  1867. change = old != new;
  1868. return change;
  1869. }
  1870. EXPORT_SYMBOL_GPL(snd_soc_test_bits);
  1871. /**
  1872. * snd_soc_cnew - create new control
  1873. * @_template: control template
  1874. * @data: control private data
  1875. * @long_name: control long name
  1876. * @prefix: control name prefix
  1877. *
  1878. * Create a new mixer control from a template control.
  1879. *
  1880. * Returns 0 for success, else error.
  1881. */
  1882. struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
  1883. void *data, const char *long_name,
  1884. const char *prefix)
  1885. {
  1886. struct snd_kcontrol_new template;
  1887. struct snd_kcontrol *kcontrol;
  1888. char *name = NULL;
  1889. memcpy(&template, _template, sizeof(template));
  1890. template.index = 0;
  1891. if (!long_name)
  1892. long_name = template.name;
  1893. if (prefix) {
  1894. name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
  1895. if (!name)
  1896. return NULL;
  1897. template.name = name;
  1898. } else {
  1899. template.name = long_name;
  1900. }
  1901. kcontrol = snd_ctl_new1(&template, data);
  1902. kfree(name);
  1903. return kcontrol;
  1904. }
  1905. EXPORT_SYMBOL_GPL(snd_soc_cnew);
  1906. static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
  1907. const struct snd_kcontrol_new *controls, int num_controls,
  1908. const char *prefix, void *data)
  1909. {
  1910. int err, i;
  1911. for (i = 0; i < num_controls; i++) {
  1912. const struct snd_kcontrol_new *control = &controls[i];
  1913. err = snd_ctl_add(card, snd_soc_cnew(control, data,
  1914. control->name, prefix));
  1915. if (err < 0) {
  1916. dev_err(dev, "ASoC: Failed to add %s: %d\n",
  1917. control->name, err);
  1918. return err;
  1919. }
  1920. }
  1921. return 0;
  1922. }
  1923. /**
  1924. * snd_soc_add_codec_controls - add an array of controls to a codec.
  1925. * Convenience function to add a list of controls. Many codecs were
  1926. * duplicating this code.
  1927. *
  1928. * @codec: codec to add controls to
  1929. * @controls: array of controls to add
  1930. * @num_controls: number of elements in the array
  1931. *
  1932. * Return 0 for success, else error.
  1933. */
  1934. int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
  1935. const struct snd_kcontrol_new *controls, int num_controls)
  1936. {
  1937. struct snd_card *card = codec->card->snd_card;
  1938. return snd_soc_add_controls(card, codec->dev, controls, num_controls,
  1939. codec->name_prefix, codec);
  1940. }
  1941. EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
  1942. /**
  1943. * snd_soc_add_platform_controls - add an array of controls to a platform.
  1944. * Convenience function to add a list of controls.
  1945. *
  1946. * @platform: platform to add controls to
  1947. * @controls: array of controls to add
  1948. * @num_controls: number of elements in the array
  1949. *
  1950. * Return 0 for success, else error.
  1951. */
  1952. int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
  1953. const struct snd_kcontrol_new *controls, int num_controls)
  1954. {
  1955. struct snd_card *card = platform->card->snd_card;
  1956. return snd_soc_add_controls(card, platform->dev, controls, num_controls,
  1957. NULL, platform);
  1958. }
  1959. EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
  1960. /**
  1961. * snd_soc_add_card_controls - add an array of controls to a SoC card.
  1962. * Convenience function to add a list of controls.
  1963. *
  1964. * @soc_card: SoC card to add controls to
  1965. * @controls: array of controls to add
  1966. * @num_controls: number of elements in the array
  1967. *
  1968. * Return 0 for success, else error.
  1969. */
  1970. int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
  1971. const struct snd_kcontrol_new *controls, int num_controls)
  1972. {
  1973. struct snd_card *card = soc_card->snd_card;
  1974. return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
  1975. NULL, soc_card);
  1976. }
  1977. EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
  1978. /**
  1979. * snd_soc_add_dai_controls - add an array of controls to a DAI.
  1980. * Convienience function to add a list of controls.
  1981. *
  1982. * @dai: DAI to add controls to
  1983. * @controls: array of controls to add
  1984. * @num_controls: number of elements in the array
  1985. *
  1986. * Return 0 for success, else error.
  1987. */
  1988. int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
  1989. const struct snd_kcontrol_new *controls, int num_controls)
  1990. {
  1991. struct snd_card *card = dai->card->snd_card;
  1992. return snd_soc_add_controls(card, dai->dev, controls, num_controls,
  1993. NULL, dai);
  1994. }
  1995. EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
  1996. /**
  1997. * snd_soc_info_enum_double - enumerated double mixer info callback
  1998. * @kcontrol: mixer control
  1999. * @uinfo: control element information
  2000. *
  2001. * Callback to provide information about a double enumerated
  2002. * mixer control.
  2003. *
  2004. * Returns 0 for success.
  2005. */
  2006. int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
  2007. struct snd_ctl_elem_info *uinfo)
  2008. {
  2009. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  2010. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  2011. uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
  2012. uinfo->value.enumerated.items = e->max;
  2013. if (uinfo->value.enumerated.item > e->max - 1)
  2014. uinfo->value.enumerated.item = e->max - 1;
  2015. strcpy(uinfo->value.enumerated.name,
  2016. e->texts[uinfo->value.enumerated.item]);
  2017. return 0;
  2018. }
  2019. EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
  2020. /**
  2021. * snd_soc_get_enum_double - enumerated double mixer get callback
  2022. * @kcontrol: mixer control
  2023. * @ucontrol: control element information
  2024. *
  2025. * Callback to get the value of a double enumerated mixer.
  2026. *
  2027. * Returns 0 for success.
  2028. */
  2029. int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
  2030. struct snd_ctl_elem_value *ucontrol)
  2031. {
  2032. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2033. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  2034. unsigned int val;
  2035. val = snd_soc_read(codec, e->reg);
  2036. ucontrol->value.enumerated.item[0]
  2037. = (val >> e->shift_l) & e->mask;
  2038. if (e->shift_l != e->shift_r)
  2039. ucontrol->value.enumerated.item[1] =
  2040. (val >> e->shift_r) & e->mask;
  2041. return 0;
  2042. }
  2043. EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
  2044. /**
  2045. * snd_soc_put_enum_double - enumerated double mixer put callback
  2046. * @kcontrol: mixer control
  2047. * @ucontrol: control element information
  2048. *
  2049. * Callback to set the value of a double enumerated mixer.
  2050. *
  2051. * Returns 0 for success.
  2052. */
  2053. int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
  2054. struct snd_ctl_elem_value *ucontrol)
  2055. {
  2056. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2057. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  2058. unsigned int val;
  2059. unsigned int mask;
  2060. if (ucontrol->value.enumerated.item[0] > e->max - 1)
  2061. return -EINVAL;
  2062. val = ucontrol->value.enumerated.item[0] << e->shift_l;
  2063. mask = e->mask << e->shift_l;
  2064. if (e->shift_l != e->shift_r) {
  2065. if (ucontrol->value.enumerated.item[1] > e->max - 1)
  2066. return -EINVAL;
  2067. val |= ucontrol->value.enumerated.item[1] << e->shift_r;
  2068. mask |= e->mask << e->shift_r;
  2069. }
  2070. return snd_soc_update_bits_locked(codec, e->reg, mask, val);
  2071. }
  2072. EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
  2073. /**
  2074. * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
  2075. * @kcontrol: mixer control
  2076. * @ucontrol: control element information
  2077. *
  2078. * Callback to get the value of a double semi enumerated mixer.
  2079. *
  2080. * Semi enumerated mixer: the enumerated items are referred as values. Can be
  2081. * used for handling bitfield coded enumeration for example.
  2082. *
  2083. * Returns 0 for success.
  2084. */
  2085. int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
  2086. struct snd_ctl_elem_value *ucontrol)
  2087. {
  2088. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2089. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  2090. unsigned int reg_val, val, mux;
  2091. reg_val = snd_soc_read(codec, e->reg);
  2092. val = (reg_val >> e->shift_l) & e->mask;
  2093. for (mux = 0; mux < e->max; mux++) {
  2094. if (val == e->values[mux])
  2095. break;
  2096. }
  2097. ucontrol->value.enumerated.item[0] = mux;
  2098. if (e->shift_l != e->shift_r) {
  2099. val = (reg_val >> e->shift_r) & e->mask;
  2100. for (mux = 0; mux < e->max; mux++) {
  2101. if (val == e->values[mux])
  2102. break;
  2103. }
  2104. ucontrol->value.enumerated.item[1] = mux;
  2105. }
  2106. return 0;
  2107. }
  2108. EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
  2109. /**
  2110. * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
  2111. * @kcontrol: mixer control
  2112. * @ucontrol: control element information
  2113. *
  2114. * Callback to set the value of a double semi enumerated mixer.
  2115. *
  2116. * Semi enumerated mixer: the enumerated items are referred as values. Can be
  2117. * used for handling bitfield coded enumeration for example.
  2118. *
  2119. * Returns 0 for success.
  2120. */
  2121. int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
  2122. struct snd_ctl_elem_value *ucontrol)
  2123. {
  2124. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2125. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  2126. unsigned int val;
  2127. unsigned int mask;
  2128. if (ucontrol->value.enumerated.item[0] > e->max - 1)
  2129. return -EINVAL;
  2130. val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
  2131. mask = e->mask << e->shift_l;
  2132. if (e->shift_l != e->shift_r) {
  2133. if (ucontrol->value.enumerated.item[1] > e->max - 1)
  2134. return -EINVAL;
  2135. val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
  2136. mask |= e->mask << e->shift_r;
  2137. }
  2138. return snd_soc_update_bits_locked(codec, e->reg, mask, val);
  2139. }
  2140. EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
  2141. /**
  2142. * snd_soc_info_enum_ext - external enumerated single mixer info callback
  2143. * @kcontrol: mixer control
  2144. * @uinfo: control element information
  2145. *
  2146. * Callback to provide information about an external enumerated
  2147. * single mixer.
  2148. *
  2149. * Returns 0 for success.
  2150. */
  2151. int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
  2152. struct snd_ctl_elem_info *uinfo)
  2153. {
  2154. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  2155. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  2156. uinfo->count = 1;
  2157. uinfo->value.enumerated.items = e->max;
  2158. if (uinfo->value.enumerated.item > e->max - 1)
  2159. uinfo->value.enumerated.item = e->max - 1;
  2160. strcpy(uinfo->value.enumerated.name,
  2161. e->texts[uinfo->value.enumerated.item]);
  2162. return 0;
  2163. }
  2164. EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
  2165. /**
  2166. * snd_soc_info_volsw_ext - external single mixer info callback
  2167. * @kcontrol: mixer control
  2168. * @uinfo: control element information
  2169. *
  2170. * Callback to provide information about a single external mixer control.
  2171. *
  2172. * Returns 0 for success.
  2173. */
  2174. int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
  2175. struct snd_ctl_elem_info *uinfo)
  2176. {
  2177. int max = kcontrol->private_value;
  2178. if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
  2179. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  2180. else
  2181. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2182. uinfo->count = 1;
  2183. uinfo->value.integer.min = 0;
  2184. uinfo->value.integer.max = max;
  2185. return 0;
  2186. }
  2187. EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
  2188. /**
  2189. * snd_soc_info_volsw - single mixer info callback
  2190. * @kcontrol: mixer control
  2191. * @uinfo: control element information
  2192. *
  2193. * Callback to provide information about a single mixer control, or a double
  2194. * mixer control that spans 2 registers.
  2195. *
  2196. * Returns 0 for success.
  2197. */
  2198. int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
  2199. struct snd_ctl_elem_info *uinfo)
  2200. {
  2201. struct soc_mixer_control *mc =
  2202. (struct soc_mixer_control *)kcontrol->private_value;
  2203. int platform_max;
  2204. if (!mc->platform_max)
  2205. mc->platform_max = mc->max;
  2206. platform_max = mc->platform_max;
  2207. if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
  2208. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  2209. else
  2210. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2211. uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
  2212. uinfo->value.integer.min = 0;
  2213. uinfo->value.integer.max = platform_max;
  2214. return 0;
  2215. }
  2216. EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
  2217. /**
  2218. * snd_soc_get_volsw - single mixer get callback
  2219. * @kcontrol: mixer control
  2220. * @ucontrol: control element information
  2221. *
  2222. * Callback to get the value of a single mixer control, or a double mixer
  2223. * control that spans 2 registers.
  2224. *
  2225. * Returns 0 for success.
  2226. */
  2227. int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
  2228. struct snd_ctl_elem_value *ucontrol)
  2229. {
  2230. struct soc_mixer_control *mc =
  2231. (struct soc_mixer_control *)kcontrol->private_value;
  2232. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2233. unsigned int reg = mc->reg;
  2234. unsigned int reg2 = mc->rreg;
  2235. unsigned int shift = mc->shift;
  2236. unsigned int rshift = mc->rshift;
  2237. int max = mc->max;
  2238. unsigned int mask = (1 << fls(max)) - 1;
  2239. unsigned int invert = mc->invert;
  2240. ucontrol->value.integer.value[0] =
  2241. (snd_soc_read(codec, reg) >> shift) & mask;
  2242. if (invert)
  2243. ucontrol->value.integer.value[0] =
  2244. max - ucontrol->value.integer.value[0];
  2245. if (snd_soc_volsw_is_stereo(mc)) {
  2246. if (reg == reg2)
  2247. ucontrol->value.integer.value[1] =
  2248. (snd_soc_read(codec, reg) >> rshift) & mask;
  2249. else
  2250. ucontrol->value.integer.value[1] =
  2251. (snd_soc_read(codec, reg2) >> shift) & mask;
  2252. if (invert)
  2253. ucontrol->value.integer.value[1] =
  2254. max - ucontrol->value.integer.value[1];
  2255. }
  2256. return 0;
  2257. }
  2258. EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
  2259. /**
  2260. * snd_soc_put_volsw - single mixer put callback
  2261. * @kcontrol: mixer control
  2262. * @ucontrol: control element information
  2263. *
  2264. * Callback to set the value of a single mixer control, or a double mixer
  2265. * control that spans 2 registers.
  2266. *
  2267. * Returns 0 for success.
  2268. */
  2269. int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
  2270. struct snd_ctl_elem_value *ucontrol)
  2271. {
  2272. struct soc_mixer_control *mc =
  2273. (struct soc_mixer_control *)kcontrol->private_value;
  2274. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2275. unsigned int reg = mc->reg;
  2276. unsigned int reg2 = mc->rreg;
  2277. unsigned int shift = mc->shift;
  2278. unsigned int rshift = mc->rshift;
  2279. int max = mc->max;
  2280. unsigned int mask = (1 << fls(max)) - 1;
  2281. unsigned int invert = mc->invert;
  2282. int err;
  2283. bool type_2r = 0;
  2284. unsigned int val2 = 0;
  2285. unsigned int val, val_mask;
  2286. val = (ucontrol->value.integer.value[0] & mask);
  2287. if (invert)
  2288. val = max - val;
  2289. val_mask = mask << shift;
  2290. val = val << shift;
  2291. if (snd_soc_volsw_is_stereo(mc)) {
  2292. val2 = (ucontrol->value.integer.value[1] & mask);
  2293. if (invert)
  2294. val2 = max - val2;
  2295. if (reg == reg2) {
  2296. val_mask |= mask << rshift;
  2297. val |= val2 << rshift;
  2298. } else {
  2299. val2 = val2 << shift;
  2300. type_2r = 1;
  2301. }
  2302. }
  2303. err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
  2304. if (err < 0)
  2305. return err;
  2306. if (type_2r)
  2307. err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
  2308. return err;
  2309. }
  2310. EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
  2311. /**
  2312. * snd_soc_get_volsw_sx - single mixer get callback
  2313. * @kcontrol: mixer control
  2314. * @ucontrol: control element information
  2315. *
  2316. * Callback to get the value of a single mixer control, or a double mixer
  2317. * control that spans 2 registers.
  2318. *
  2319. * Returns 0 for success.
  2320. */
  2321. int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
  2322. struct snd_ctl_elem_value *ucontrol)
  2323. {
  2324. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2325. struct soc_mixer_control *mc =
  2326. (struct soc_mixer_control *)kcontrol->private_value;
  2327. unsigned int reg = mc->reg;
  2328. unsigned int reg2 = mc->rreg;
  2329. unsigned int shift = mc->shift;
  2330. unsigned int rshift = mc->rshift;
  2331. int max = mc->max;
  2332. int min = mc->min;
  2333. int mask = (1 << (fls(min + max) - 1)) - 1;
  2334. ucontrol->value.integer.value[0] =
  2335. ((snd_soc_read(codec, reg) >> shift) - min) & mask;
  2336. if (snd_soc_volsw_is_stereo(mc))
  2337. ucontrol->value.integer.value[1] =
  2338. ((snd_soc_read(codec, reg2) >> rshift) - min) & mask;
  2339. return 0;
  2340. }
  2341. EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
  2342. /**
  2343. * snd_soc_put_volsw_sx - double mixer set callback
  2344. * @kcontrol: mixer control
  2345. * @uinfo: control element information
  2346. *
  2347. * Callback to set the value of a double mixer control that spans 2 registers.
  2348. *
  2349. * Returns 0 for success.
  2350. */
  2351. int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
  2352. struct snd_ctl_elem_value *ucontrol)
  2353. {
  2354. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2355. struct soc_mixer_control *mc =
  2356. (struct soc_mixer_control *)kcontrol->private_value;
  2357. unsigned int reg = mc->reg;
  2358. unsigned int reg2 = mc->rreg;
  2359. unsigned int shift = mc->shift;
  2360. unsigned int rshift = mc->rshift;
  2361. int max = mc->max;
  2362. int min = mc->min;
  2363. int mask = (1 << (fls(min + max) - 1)) - 1;
  2364. int err = 0;
  2365. unsigned short val, val_mask, val2 = 0;
  2366. val_mask = mask << shift;
  2367. val = (ucontrol->value.integer.value[0] + min) & mask;
  2368. val = val << shift;
  2369. err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
  2370. if (err < 0)
  2371. return err;
  2372. if (snd_soc_volsw_is_stereo(mc)) {
  2373. val_mask = mask << rshift;
  2374. val2 = (ucontrol->value.integer.value[1] + min) & mask;
  2375. val2 = val2 << rshift;
  2376. if (snd_soc_update_bits_locked(codec, reg2, val_mask, val2))
  2377. return err;
  2378. }
  2379. return 0;
  2380. }
  2381. EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
  2382. /**
  2383. * snd_soc_info_volsw_s8 - signed mixer info callback
  2384. * @kcontrol: mixer control
  2385. * @uinfo: control element information
  2386. *
  2387. * Callback to provide information about a signed mixer control.
  2388. *
  2389. * Returns 0 for success.
  2390. */
  2391. int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
  2392. struct snd_ctl_elem_info *uinfo)
  2393. {
  2394. struct soc_mixer_control *mc =
  2395. (struct soc_mixer_control *)kcontrol->private_value;
  2396. int platform_max;
  2397. int min = mc->min;
  2398. if (!mc->platform_max)
  2399. mc->platform_max = mc->max;
  2400. platform_max = mc->platform_max;
  2401. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2402. uinfo->count = 2;
  2403. uinfo->value.integer.min = 0;
  2404. uinfo->value.integer.max = platform_max - min;
  2405. return 0;
  2406. }
  2407. EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
  2408. /**
  2409. * snd_soc_get_volsw_s8 - signed mixer get callback
  2410. * @kcontrol: mixer control
  2411. * @ucontrol: control element information
  2412. *
  2413. * Callback to get the value of a signed mixer control.
  2414. *
  2415. * Returns 0 for success.
  2416. */
  2417. int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
  2418. struct snd_ctl_elem_value *ucontrol)
  2419. {
  2420. struct soc_mixer_control *mc =
  2421. (struct soc_mixer_control *)kcontrol->private_value;
  2422. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2423. unsigned int reg = mc->reg;
  2424. int min = mc->min;
  2425. int val = snd_soc_read(codec, reg);
  2426. ucontrol->value.integer.value[0] =
  2427. ((signed char)(val & 0xff))-min;
  2428. ucontrol->value.integer.value[1] =
  2429. ((signed char)((val >> 8) & 0xff))-min;
  2430. return 0;
  2431. }
  2432. EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
  2433. /**
  2434. * snd_soc_put_volsw_sgn - signed mixer put callback
  2435. * @kcontrol: mixer control
  2436. * @ucontrol: control element information
  2437. *
  2438. * Callback to set the value of a signed mixer control.
  2439. *
  2440. * Returns 0 for success.
  2441. */
  2442. int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
  2443. struct snd_ctl_elem_value *ucontrol)
  2444. {
  2445. struct soc_mixer_control *mc =
  2446. (struct soc_mixer_control *)kcontrol->private_value;
  2447. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2448. unsigned int reg = mc->reg;
  2449. int min = mc->min;
  2450. unsigned int val;
  2451. val = (ucontrol->value.integer.value[0]+min) & 0xff;
  2452. val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
  2453. return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
  2454. }
  2455. EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
  2456. /**
  2457. * snd_soc_info_volsw_range - single mixer info callback with range.
  2458. * @kcontrol: mixer control
  2459. * @uinfo: control element information
  2460. *
  2461. * Callback to provide information, within a range, about a single
  2462. * mixer control.
  2463. *
  2464. * returns 0 for success.
  2465. */
  2466. int snd_soc_info_volsw_range(struct snd_kcontrol *kcontrol,
  2467. struct snd_ctl_elem_info *uinfo)
  2468. {
  2469. struct soc_mixer_control *mc =
  2470. (struct soc_mixer_control *)kcontrol->private_value;
  2471. int platform_max;
  2472. int min = mc->min;
  2473. if (!mc->platform_max)
  2474. mc->platform_max = mc->max;
  2475. platform_max = mc->platform_max;
  2476. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2477. uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
  2478. uinfo->value.integer.min = 0;
  2479. uinfo->value.integer.max = platform_max - min;
  2480. return 0;
  2481. }
  2482. EXPORT_SYMBOL_GPL(snd_soc_info_volsw_range);
  2483. /**
  2484. * snd_soc_put_volsw_range - single mixer put value callback with range.
  2485. * @kcontrol: mixer control
  2486. * @ucontrol: control element information
  2487. *
  2488. * Callback to set the value, within a range, for a single mixer control.
  2489. *
  2490. * Returns 0 for success.
  2491. */
  2492. int snd_soc_put_volsw_range(struct snd_kcontrol *kcontrol,
  2493. struct snd_ctl_elem_value *ucontrol)
  2494. {
  2495. struct soc_mixer_control *mc =
  2496. (struct soc_mixer_control *)kcontrol->private_value;
  2497. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2498. unsigned int reg = mc->reg;
  2499. unsigned int rreg = mc->rreg;
  2500. unsigned int shift = mc->shift;
  2501. int min = mc->min;
  2502. int max = mc->max;
  2503. unsigned int mask = (1 << fls(max)) - 1;
  2504. unsigned int invert = mc->invert;
  2505. unsigned int val, val_mask;
  2506. int ret;
  2507. val = ((ucontrol->value.integer.value[0] + min) & mask);
  2508. if (invert)
  2509. val = max - val;
  2510. val_mask = mask << shift;
  2511. val = val << shift;
  2512. ret = snd_soc_update_bits_locked(codec, reg, val_mask, val);
  2513. if (ret < 0)
  2514. return ret;
  2515. if (snd_soc_volsw_is_stereo(mc)) {
  2516. val = ((ucontrol->value.integer.value[1] + min) & mask);
  2517. if (invert)
  2518. val = max - val;
  2519. val_mask = mask << shift;
  2520. val = val << shift;
  2521. ret = snd_soc_update_bits_locked(codec, rreg, val_mask, val);
  2522. }
  2523. return ret;
  2524. }
  2525. EXPORT_SYMBOL_GPL(snd_soc_put_volsw_range);
  2526. /**
  2527. * snd_soc_get_volsw_range - single mixer get callback with range
  2528. * @kcontrol: mixer control
  2529. * @ucontrol: control element information
  2530. *
  2531. * Callback to get the value, within a range, of a single mixer control.
  2532. *
  2533. * Returns 0 for success.
  2534. */
  2535. int snd_soc_get_volsw_range(struct snd_kcontrol *kcontrol,
  2536. struct snd_ctl_elem_value *ucontrol)
  2537. {
  2538. struct soc_mixer_control *mc =
  2539. (struct soc_mixer_control *)kcontrol->private_value;
  2540. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2541. unsigned int reg = mc->reg;
  2542. unsigned int rreg = mc->rreg;
  2543. unsigned int shift = mc->shift;
  2544. int min = mc->min;
  2545. int max = mc->max;
  2546. unsigned int mask = (1 << fls(max)) - 1;
  2547. unsigned int invert = mc->invert;
  2548. ucontrol->value.integer.value[0] =
  2549. (snd_soc_read(codec, reg) >> shift) & mask;
  2550. if (invert)
  2551. ucontrol->value.integer.value[0] =
  2552. max - ucontrol->value.integer.value[0];
  2553. ucontrol->value.integer.value[0] =
  2554. ucontrol->value.integer.value[0] - min;
  2555. if (snd_soc_volsw_is_stereo(mc)) {
  2556. ucontrol->value.integer.value[1] =
  2557. (snd_soc_read(codec, rreg) >> shift) & mask;
  2558. if (invert)
  2559. ucontrol->value.integer.value[1] =
  2560. max - ucontrol->value.integer.value[1];
  2561. ucontrol->value.integer.value[1] =
  2562. ucontrol->value.integer.value[1] - min;
  2563. }
  2564. return 0;
  2565. }
  2566. EXPORT_SYMBOL_GPL(snd_soc_get_volsw_range);
  2567. /**
  2568. * snd_soc_limit_volume - Set new limit to an existing volume control.
  2569. *
  2570. * @codec: where to look for the control
  2571. * @name: Name of the control
  2572. * @max: new maximum limit
  2573. *
  2574. * Return 0 for success, else error.
  2575. */
  2576. int snd_soc_limit_volume(struct snd_soc_codec *codec,
  2577. const char *name, int max)
  2578. {
  2579. struct snd_card *card = codec->card->snd_card;
  2580. struct snd_kcontrol *kctl;
  2581. struct soc_mixer_control *mc;
  2582. int found = 0;
  2583. int ret = -EINVAL;
  2584. /* Sanity check for name and max */
  2585. if (unlikely(!name || max <= 0))
  2586. return -EINVAL;
  2587. list_for_each_entry(kctl, &card->controls, list) {
  2588. if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
  2589. found = 1;
  2590. break;
  2591. }
  2592. }
  2593. if (found) {
  2594. mc = (struct soc_mixer_control *)kctl->private_value;
  2595. if (max <= mc->max) {
  2596. mc->platform_max = max;
  2597. ret = 0;
  2598. }
  2599. }
  2600. return ret;
  2601. }
  2602. EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
  2603. int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
  2604. struct snd_ctl_elem_info *uinfo)
  2605. {
  2606. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2607. struct soc_bytes *params = (void *)kcontrol->private_value;
  2608. uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
  2609. uinfo->count = params->num_regs * codec->val_bytes;
  2610. return 0;
  2611. }
  2612. EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
  2613. int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
  2614. struct snd_ctl_elem_value *ucontrol)
  2615. {
  2616. struct soc_bytes *params = (void *)kcontrol->private_value;
  2617. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2618. int ret;
  2619. if (codec->using_regmap)
  2620. ret = regmap_raw_read(codec->control_data, params->base,
  2621. ucontrol->value.bytes.data,
  2622. params->num_regs * codec->val_bytes);
  2623. else
  2624. ret = -EINVAL;
  2625. /* Hide any masked bytes to ensure consistent data reporting */
  2626. if (ret == 0 && params->mask) {
  2627. switch (codec->val_bytes) {
  2628. case 1:
  2629. ucontrol->value.bytes.data[0] &= ~params->mask;
  2630. break;
  2631. case 2:
  2632. ((u16 *)(&ucontrol->value.bytes.data))[0]
  2633. &= ~params->mask;
  2634. break;
  2635. case 4:
  2636. ((u32 *)(&ucontrol->value.bytes.data))[0]
  2637. &= ~params->mask;
  2638. break;
  2639. default:
  2640. return -EINVAL;
  2641. }
  2642. }
  2643. return ret;
  2644. }
  2645. EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
  2646. int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
  2647. struct snd_ctl_elem_value *ucontrol)
  2648. {
  2649. struct soc_bytes *params = (void *)kcontrol->private_value;
  2650. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2651. int ret, len;
  2652. unsigned int val;
  2653. void *data;
  2654. if (!codec->using_regmap)
  2655. return -EINVAL;
  2656. len = params->num_regs * codec->val_bytes;
  2657. data = kmemdup(ucontrol->value.bytes.data, len, GFP_KERNEL | GFP_DMA);
  2658. if (!data)
  2659. return -ENOMEM;
  2660. /*
  2661. * If we've got a mask then we need to preserve the register
  2662. * bits. We shouldn't modify the incoming data so take a
  2663. * copy.
  2664. */
  2665. if (params->mask) {
  2666. ret = regmap_read(codec->control_data, params->base, &val);
  2667. if (ret != 0)
  2668. goto out;
  2669. val &= params->mask;
  2670. switch (codec->val_bytes) {
  2671. case 1:
  2672. ((u8 *)data)[0] &= ~params->mask;
  2673. ((u8 *)data)[0] |= val;
  2674. break;
  2675. case 2:
  2676. ((u16 *)data)[0] &= cpu_to_be16(~params->mask);
  2677. ((u16 *)data)[0] |= cpu_to_be16(val);
  2678. break;
  2679. case 4:
  2680. ((u32 *)data)[0] &= cpu_to_be32(~params->mask);
  2681. ((u32 *)data)[0] |= cpu_to_be32(val);
  2682. break;
  2683. default:
  2684. ret = -EINVAL;
  2685. goto out;
  2686. }
  2687. }
  2688. ret = regmap_raw_write(codec->control_data, params->base,
  2689. data, len);
  2690. out:
  2691. kfree(data);
  2692. return ret;
  2693. }
  2694. EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
  2695. /**
  2696. * snd_soc_info_xr_sx - signed multi register info callback
  2697. * @kcontrol: mreg control
  2698. * @uinfo: control element information
  2699. *
  2700. * Callback to provide information of a control that can
  2701. * span multiple codec registers which together
  2702. * forms a single signed value in a MSB/LSB manner.
  2703. *
  2704. * Returns 0 for success.
  2705. */
  2706. int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol,
  2707. struct snd_ctl_elem_info *uinfo)
  2708. {
  2709. struct soc_mreg_control *mc =
  2710. (struct soc_mreg_control *)kcontrol->private_value;
  2711. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2712. uinfo->count = 1;
  2713. uinfo->value.integer.min = mc->min;
  2714. uinfo->value.integer.max = mc->max;
  2715. return 0;
  2716. }
  2717. EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx);
  2718. /**
  2719. * snd_soc_get_xr_sx - signed multi register get callback
  2720. * @kcontrol: mreg control
  2721. * @ucontrol: control element information
  2722. *
  2723. * Callback to get the value of a control that can span
  2724. * multiple codec registers which together forms a single
  2725. * signed value in a MSB/LSB manner. The control supports
  2726. * specifying total no of bits used to allow for bitfields
  2727. * across the multiple codec registers.
  2728. *
  2729. * Returns 0 for success.
  2730. */
  2731. int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol,
  2732. struct snd_ctl_elem_value *ucontrol)
  2733. {
  2734. struct soc_mreg_control *mc =
  2735. (struct soc_mreg_control *)kcontrol->private_value;
  2736. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2737. unsigned int regbase = mc->regbase;
  2738. unsigned int regcount = mc->regcount;
  2739. unsigned int regwshift = codec->driver->reg_word_size * BITS_PER_BYTE;
  2740. unsigned int regwmask = (1<<regwshift)-1;
  2741. unsigned int invert = mc->invert;
  2742. unsigned long mask = (1UL<<mc->nbits)-1;
  2743. long min = mc->min;
  2744. long max = mc->max;
  2745. long val = 0;
  2746. unsigned long regval;
  2747. unsigned int i;
  2748. for (i = 0; i < regcount; i++) {
  2749. regval = snd_soc_read(codec, regbase+i) & regwmask;
  2750. val |= regval << (regwshift*(regcount-i-1));
  2751. }
  2752. val &= mask;
  2753. if (min < 0 && val > max)
  2754. val |= ~mask;
  2755. if (invert)
  2756. val = max - val;
  2757. ucontrol->value.integer.value[0] = val;
  2758. return 0;
  2759. }
  2760. EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx);
  2761. /**
  2762. * snd_soc_put_xr_sx - signed multi register get callback
  2763. * @kcontrol: mreg control
  2764. * @ucontrol: control element information
  2765. *
  2766. * Callback to set the value of a control that can span
  2767. * multiple codec registers which together forms a single
  2768. * signed value in a MSB/LSB manner. The control supports
  2769. * specifying total no of bits used to allow for bitfields
  2770. * across the multiple codec registers.
  2771. *
  2772. * Returns 0 for success.
  2773. */
  2774. int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol,
  2775. struct snd_ctl_elem_value *ucontrol)
  2776. {
  2777. struct soc_mreg_control *mc =
  2778. (struct soc_mreg_control *)kcontrol->private_value;
  2779. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2780. unsigned int regbase = mc->regbase;
  2781. unsigned int regcount = mc->regcount;
  2782. unsigned int regwshift = codec->driver->reg_word_size * BITS_PER_BYTE;
  2783. unsigned int regwmask = (1<<regwshift)-1;
  2784. unsigned int invert = mc->invert;
  2785. unsigned long mask = (1UL<<mc->nbits)-1;
  2786. long max = mc->max;
  2787. long val = ucontrol->value.integer.value[0];
  2788. unsigned int i, regval, regmask;
  2789. int err;
  2790. if (invert)
  2791. val = max - val;
  2792. val &= mask;
  2793. for (i = 0; i < regcount; i++) {
  2794. regval = (val >> (regwshift*(regcount-i-1))) & regwmask;
  2795. regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask;
  2796. err = snd_soc_update_bits_locked(codec, regbase+i,
  2797. regmask, regval);
  2798. if (err < 0)
  2799. return err;
  2800. }
  2801. return 0;
  2802. }
  2803. EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx);
  2804. /**
  2805. * snd_soc_get_strobe - strobe get callback
  2806. * @kcontrol: mixer control
  2807. * @ucontrol: control element information
  2808. *
  2809. * Callback get the value of a strobe mixer control.
  2810. *
  2811. * Returns 0 for success.
  2812. */
  2813. int snd_soc_get_strobe(struct snd_kcontrol *kcontrol,
  2814. struct snd_ctl_elem_value *ucontrol)
  2815. {
  2816. struct soc_mixer_control *mc =
  2817. (struct soc_mixer_control *)kcontrol->private_value;
  2818. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2819. unsigned int reg = mc->reg;
  2820. unsigned int shift = mc->shift;
  2821. unsigned int mask = 1 << shift;
  2822. unsigned int invert = mc->invert != 0;
  2823. unsigned int val = snd_soc_read(codec, reg) & mask;
  2824. if (shift != 0 && val != 0)
  2825. val = val >> shift;
  2826. ucontrol->value.enumerated.item[0] = val ^ invert;
  2827. return 0;
  2828. }
  2829. EXPORT_SYMBOL_GPL(snd_soc_get_strobe);
  2830. /**
  2831. * snd_soc_put_strobe - strobe put callback
  2832. * @kcontrol: mixer control
  2833. * @ucontrol: control element information
  2834. *
  2835. * Callback strobe a register bit to high then low (or the inverse)
  2836. * in one pass of a single mixer enum control.
  2837. *
  2838. * Returns 1 for success.
  2839. */
  2840. int snd_soc_put_strobe(struct snd_kcontrol *kcontrol,
  2841. struct snd_ctl_elem_value *ucontrol)
  2842. {
  2843. struct soc_mixer_control *mc =
  2844. (struct soc_mixer_control *)kcontrol->private_value;
  2845. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2846. unsigned int reg = mc->reg;
  2847. unsigned int shift = mc->shift;
  2848. unsigned int mask = 1 << shift;
  2849. unsigned int invert = mc->invert != 0;
  2850. unsigned int strobe = ucontrol->value.enumerated.item[0] != 0;
  2851. unsigned int val1 = (strobe ^ invert) ? mask : 0;
  2852. unsigned int val2 = (strobe ^ invert) ? 0 : mask;
  2853. int err;
  2854. err = snd_soc_update_bits_locked(codec, reg, mask, val1);
  2855. if (err < 0)
  2856. return err;
  2857. err = snd_soc_update_bits_locked(codec, reg, mask, val2);
  2858. return err;
  2859. }
  2860. EXPORT_SYMBOL_GPL(snd_soc_put_strobe);
  2861. /**
  2862. * snd_soc_dai_set_sysclk - configure DAI system or master clock.
  2863. * @dai: DAI
  2864. * @clk_id: DAI specific clock ID
  2865. * @freq: new clock frequency in Hz
  2866. * @dir: new clock direction - input/output.
  2867. *
  2868. * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
  2869. */
  2870. int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
  2871. unsigned int freq, int dir)
  2872. {
  2873. if (dai->driver && dai->driver->ops->set_sysclk)
  2874. return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
  2875. else if (dai->codec && dai->codec->driver->set_sysclk)
  2876. return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
  2877. freq, dir);
  2878. else
  2879. return -EINVAL;
  2880. }
  2881. EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
  2882. /**
  2883. * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
  2884. * @codec: CODEC
  2885. * @clk_id: DAI specific clock ID
  2886. * @source: Source for the clock
  2887. * @freq: new clock frequency in Hz
  2888. * @dir: new clock direction - input/output.
  2889. *
  2890. * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
  2891. */
  2892. int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
  2893. int source, unsigned int freq, int dir)
  2894. {
  2895. if (codec->driver->set_sysclk)
  2896. return codec->driver->set_sysclk(codec, clk_id, source,
  2897. freq, dir);
  2898. else
  2899. return -EINVAL;
  2900. }
  2901. EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
  2902. /**
  2903. * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
  2904. * @dai: DAI
  2905. * @div_id: DAI specific clock divider ID
  2906. * @div: new clock divisor.
  2907. *
  2908. * Configures the clock dividers. This is used to derive the best DAI bit and
  2909. * frame clocks from the system or master clock. It's best to set the DAI bit
  2910. * and frame clocks as low as possible to save system power.
  2911. */
  2912. int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
  2913. int div_id, int div)
  2914. {
  2915. if (dai->driver && dai->driver->ops->set_clkdiv)
  2916. return dai->driver->ops->set_clkdiv(dai, div_id, div);
  2917. else
  2918. return -EINVAL;
  2919. }
  2920. EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
  2921. /**
  2922. * snd_soc_dai_set_pll - configure DAI PLL.
  2923. * @dai: DAI
  2924. * @pll_id: DAI specific PLL ID
  2925. * @source: DAI specific source for the PLL
  2926. * @freq_in: PLL input clock frequency in Hz
  2927. * @freq_out: requested PLL output clock frequency in Hz
  2928. *
  2929. * Configures and enables PLL to generate output clock based on input clock.
  2930. */
  2931. int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
  2932. unsigned int freq_in, unsigned int freq_out)
  2933. {
  2934. if (dai->driver && dai->driver->ops->set_pll)
  2935. return dai->driver->ops->set_pll(dai, pll_id, source,
  2936. freq_in, freq_out);
  2937. else if (dai->codec && dai->codec->driver->set_pll)
  2938. return dai->codec->driver->set_pll(dai->codec, pll_id, source,
  2939. freq_in, freq_out);
  2940. else
  2941. return -EINVAL;
  2942. }
  2943. EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
  2944. /*
  2945. * snd_soc_codec_set_pll - configure codec PLL.
  2946. * @codec: CODEC
  2947. * @pll_id: DAI specific PLL ID
  2948. * @source: DAI specific source for the PLL
  2949. * @freq_in: PLL input clock frequency in Hz
  2950. * @freq_out: requested PLL output clock frequency in Hz
  2951. *
  2952. * Configures and enables PLL to generate output clock based on input clock.
  2953. */
  2954. int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
  2955. unsigned int freq_in, unsigned int freq_out)
  2956. {
  2957. if (codec->driver->set_pll)
  2958. return codec->driver->set_pll(codec, pll_id, source,
  2959. freq_in, freq_out);
  2960. else
  2961. return -EINVAL;
  2962. }
  2963. EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
  2964. /**
  2965. * snd_soc_dai_set_fmt - configure DAI hardware audio format.
  2966. * @dai: DAI
  2967. * @fmt: SND_SOC_DAIFMT_ format value.
  2968. *
  2969. * Configures the DAI hardware format and clocking.
  2970. */
  2971. int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  2972. {
  2973. if (dai->driver == NULL)
  2974. return -EINVAL;
  2975. if (dai->driver->ops->set_fmt == NULL)
  2976. return -ENOTSUPP;
  2977. return dai->driver->ops->set_fmt(dai, fmt);
  2978. }
  2979. EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
  2980. /**
  2981. * snd_soc_dai_set_tdm_slot - configure DAI TDM.
  2982. * @dai: DAI
  2983. * @tx_mask: bitmask representing active TX slots.
  2984. * @rx_mask: bitmask representing active RX slots.
  2985. * @slots: Number of slots in use.
  2986. * @slot_width: Width in bits for each slot.
  2987. *
  2988. * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
  2989. * specific.
  2990. */
  2991. int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
  2992. unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
  2993. {
  2994. if (dai->driver && dai->driver->ops->set_tdm_slot)
  2995. return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
  2996. slots, slot_width);
  2997. else
  2998. return -EINVAL;
  2999. }
  3000. EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
  3001. /**
  3002. * snd_soc_dai_set_channel_map - configure DAI audio channel map
  3003. * @dai: DAI
  3004. * @tx_num: how many TX channels
  3005. * @tx_slot: pointer to an array which imply the TX slot number channel
  3006. * 0~num-1 uses
  3007. * @rx_num: how many RX channels
  3008. * @rx_slot: pointer to an array which imply the RX slot number channel
  3009. * 0~num-1 uses
  3010. *
  3011. * configure the relationship between channel number and TDM slot number.
  3012. */
  3013. int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
  3014. unsigned int tx_num, unsigned int *tx_slot,
  3015. unsigned int rx_num, unsigned int *rx_slot)
  3016. {
  3017. if (dai->driver && dai->driver->ops->set_channel_map)
  3018. return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
  3019. rx_num, rx_slot);
  3020. else
  3021. return -EINVAL;
  3022. }
  3023. EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
  3024. /**
  3025. * snd_soc_dai_set_tristate - configure DAI system or master clock.
  3026. * @dai: DAI
  3027. * @tristate: tristate enable
  3028. *
  3029. * Tristates the DAI so that others can use it.
  3030. */
  3031. int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
  3032. {
  3033. if (dai->driver && dai->driver->ops->set_tristate)
  3034. return dai->driver->ops->set_tristate(dai, tristate);
  3035. else
  3036. return -EINVAL;
  3037. }
  3038. EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
  3039. /**
  3040. * snd_soc_dai_digital_mute - configure DAI system or master clock.
  3041. * @dai: DAI
  3042. * @mute: mute enable
  3043. * @direction: stream to mute
  3044. *
  3045. * Mutes the DAI DAC.
  3046. */
  3047. int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
  3048. int direction)
  3049. {
  3050. if (!dai->driver)
  3051. return -ENOTSUPP;
  3052. if (dai->driver->ops->mute_stream)
  3053. return dai->driver->ops->mute_stream(dai, mute, direction);
  3054. else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
  3055. dai->driver->ops->digital_mute)
  3056. return dai->driver->ops->digital_mute(dai, mute);
  3057. else
  3058. return -ENOTSUPP;
  3059. }
  3060. EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
  3061. /**
  3062. * snd_soc_register_card - Register a card with the ASoC core
  3063. *
  3064. * @card: Card to register
  3065. *
  3066. */
  3067. int snd_soc_register_card(struct snd_soc_card *card)
  3068. {
  3069. int i, ret;
  3070. if (!card->name || !card->dev)
  3071. return -EINVAL;
  3072. for (i = 0; i < card->num_links; i++) {
  3073. struct snd_soc_dai_link *link = &card->dai_link[i];
  3074. /*
  3075. * Codec must be specified by 1 of name or OF node,
  3076. * not both or neither.
  3077. */
  3078. if (!!link->codec_name == !!link->codec_of_node) {
  3079. dev_err(card->dev,
  3080. "ASoC: Neither/both codec name/of_node are set for %s\n",
  3081. link->name);
  3082. return -EINVAL;
  3083. }
  3084. /* Codec DAI name must be specified */
  3085. if (!link->codec_dai_name) {
  3086. dev_err(card->dev,
  3087. "ASoC: codec_dai_name not set for %s\n",
  3088. link->name);
  3089. return -EINVAL;
  3090. }
  3091. /*
  3092. * Platform may be specified by either name or OF node, but
  3093. * can be left unspecified, and a dummy platform will be used.
  3094. */
  3095. if (link->platform_name && link->platform_of_node) {
  3096. dev_err(card->dev,
  3097. "ASoC: Both platform name/of_node are set for %s\n",
  3098. link->name);
  3099. return -EINVAL;
  3100. }
  3101. /*
  3102. * CPU device may be specified by either name or OF node, but
  3103. * can be left unspecified, and will be matched based on DAI
  3104. * name alone..
  3105. */
  3106. if (link->cpu_name && link->cpu_of_node) {
  3107. dev_err(card->dev,
  3108. "ASoC: Neither/both cpu name/of_node are set for %s\n",
  3109. link->name);
  3110. return -EINVAL;
  3111. }
  3112. /*
  3113. * At least one of CPU DAI name or CPU device name/node must be
  3114. * specified
  3115. */
  3116. if (!link->cpu_dai_name &&
  3117. !(link->cpu_name || link->cpu_of_node)) {
  3118. dev_err(card->dev,
  3119. "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
  3120. link->name);
  3121. return -EINVAL;
  3122. }
  3123. }
  3124. dev_set_drvdata(card->dev, card);
  3125. snd_soc_initialize_card_lists(card);
  3126. soc_init_card_debugfs(card);
  3127. card->rtd = devm_kzalloc(card->dev,
  3128. sizeof(struct snd_soc_pcm_runtime) *
  3129. (card->num_links + card->num_aux_devs),
  3130. GFP_KERNEL);
  3131. if (card->rtd == NULL)
  3132. return -ENOMEM;
  3133. card->num_rtd = 0;
  3134. card->rtd_aux = &card->rtd[card->num_links];
  3135. for (i = 0; i < card->num_links; i++)
  3136. card->rtd[i].dai_link = &card->dai_link[i];
  3137. INIT_LIST_HEAD(&card->list);
  3138. INIT_LIST_HEAD(&card->dapm_dirty);
  3139. card->instantiated = 0;
  3140. mutex_init(&card->mutex);
  3141. mutex_init(&card->dapm_mutex);
  3142. ret = snd_soc_instantiate_card(card);
  3143. if (ret != 0)
  3144. soc_cleanup_card_debugfs(card);
  3145. return ret;
  3146. }
  3147. EXPORT_SYMBOL_GPL(snd_soc_register_card);
  3148. /**
  3149. * snd_soc_unregister_card - Unregister a card with the ASoC core
  3150. *
  3151. * @card: Card to unregister
  3152. *
  3153. */
  3154. int snd_soc_unregister_card(struct snd_soc_card *card)
  3155. {
  3156. if (card->instantiated)
  3157. soc_cleanup_card_resources(card);
  3158. dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
  3159. return 0;
  3160. }
  3161. EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
  3162. /*
  3163. * Simplify DAI link configuration by removing ".-1" from device names
  3164. * and sanitizing names.
  3165. */
  3166. static char *fmt_single_name(struct device *dev, int *id)
  3167. {
  3168. char *found, name[NAME_SIZE];
  3169. int id1, id2;
  3170. if (dev_name(dev) == NULL)
  3171. return NULL;
  3172. strlcpy(name, dev_name(dev), NAME_SIZE);
  3173. /* are we a "%s.%d" name (platform and SPI components) */
  3174. found = strstr(name, dev->driver->name);
  3175. if (found) {
  3176. /* get ID */
  3177. if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
  3178. /* discard ID from name if ID == -1 */
  3179. if (*id == -1)
  3180. found[strlen(dev->driver->name)] = '\0';
  3181. }
  3182. } else {
  3183. /* I2C component devices are named "bus-addr" */
  3184. if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
  3185. char tmp[NAME_SIZE];
  3186. /* create unique ID number from I2C addr and bus */
  3187. *id = ((id1 & 0xffff) << 16) + id2;
  3188. /* sanitize component name for DAI link creation */
  3189. snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
  3190. strlcpy(name, tmp, NAME_SIZE);
  3191. } else
  3192. *id = 0;
  3193. }
  3194. return kstrdup(name, GFP_KERNEL);
  3195. }
  3196. /*
  3197. * Simplify DAI link naming for single devices with multiple DAIs by removing
  3198. * any ".-1" and using the DAI name (instead of device name).
  3199. */
  3200. static inline char *fmt_multiple_name(struct device *dev,
  3201. struct snd_soc_dai_driver *dai_drv)
  3202. {
  3203. if (dai_drv->name == NULL) {
  3204. dev_err(dev,
  3205. "ASoC: error - multiple DAI %s registered with no name\n",
  3206. dev_name(dev));
  3207. return NULL;
  3208. }
  3209. return kstrdup(dai_drv->name, GFP_KERNEL);
  3210. }
  3211. /**
  3212. * snd_soc_register_dai - Register a DAI with the ASoC core
  3213. *
  3214. * @dai: DAI to register
  3215. */
  3216. static int snd_soc_register_dai(struct device *dev,
  3217. struct snd_soc_dai_driver *dai_drv)
  3218. {
  3219. struct snd_soc_codec *codec;
  3220. struct snd_soc_dai *dai;
  3221. dev_dbg(dev, "ASoC: dai register %s\n", dev_name(dev));
  3222. dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
  3223. if (dai == NULL)
  3224. return -ENOMEM;
  3225. /* create DAI component name */
  3226. dai->name = fmt_single_name(dev, &dai->id);
  3227. if (dai->name == NULL) {
  3228. kfree(dai);
  3229. return -ENOMEM;
  3230. }
  3231. dai->dev = dev;
  3232. dai->driver = dai_drv;
  3233. dai->dapm.dev = dev;
  3234. if (!dai->driver->ops)
  3235. dai->driver->ops = &null_dai_ops;
  3236. mutex_lock(&client_mutex);
  3237. list_for_each_entry(codec, &codec_list, list) {
  3238. if (codec->dev == dev) {
  3239. dev_dbg(dev, "ASoC: Mapped DAI %s to CODEC %s\n",
  3240. dai->name, codec->name);
  3241. dai->codec = codec;
  3242. break;
  3243. }
  3244. }
  3245. if (!dai->codec)
  3246. dai->dapm.idle_bias_off = 1;
  3247. list_add(&dai->list, &dai_list);
  3248. mutex_unlock(&client_mutex);
  3249. dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
  3250. return 0;
  3251. }
  3252. /**
  3253. * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
  3254. *
  3255. * @dai: DAI to unregister
  3256. */
  3257. static void snd_soc_unregister_dai(struct device *dev)
  3258. {
  3259. struct snd_soc_dai *dai;
  3260. list_for_each_entry(dai, &dai_list, list) {
  3261. if (dev == dai->dev)
  3262. goto found;
  3263. }
  3264. return;
  3265. found:
  3266. mutex_lock(&client_mutex);
  3267. list_del(&dai->list);
  3268. mutex_unlock(&client_mutex);
  3269. dev_dbg(dev, "ASoC: Unregistered DAI '%s'\n", dai->name);
  3270. kfree(dai->name);
  3271. kfree(dai);
  3272. }
  3273. /**
  3274. * snd_soc_register_dais - Register multiple DAIs with the ASoC core
  3275. *
  3276. * @dai: Array of DAIs to register
  3277. * @count: Number of DAIs
  3278. */
  3279. static int snd_soc_register_dais(struct device *dev,
  3280. struct snd_soc_dai_driver *dai_drv, size_t count)
  3281. {
  3282. struct snd_soc_codec *codec;
  3283. struct snd_soc_dai *dai;
  3284. int i, ret = 0;
  3285. dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count);
  3286. for (i = 0; i < count; i++) {
  3287. dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
  3288. if (dai == NULL) {
  3289. ret = -ENOMEM;
  3290. goto err;
  3291. }
  3292. /* create DAI component name */
  3293. dai->name = fmt_multiple_name(dev, &dai_drv[i]);
  3294. if (dai->name == NULL) {
  3295. kfree(dai);
  3296. ret = -EINVAL;
  3297. goto err;
  3298. }
  3299. dai->dev = dev;
  3300. dai->driver = &dai_drv[i];
  3301. if (dai->driver->id)
  3302. dai->id = dai->driver->id;
  3303. else
  3304. dai->id = i;
  3305. dai->dapm.dev = dev;
  3306. if (!dai->driver->ops)
  3307. dai->driver->ops = &null_dai_ops;
  3308. mutex_lock(&client_mutex);
  3309. list_for_each_entry(codec, &codec_list, list) {
  3310. if (codec->dev == dev) {
  3311. dev_dbg(dev,
  3312. "ASoC: Mapped DAI %s to CODEC %s\n",
  3313. dai->name, codec->name);
  3314. dai->codec = codec;
  3315. break;
  3316. }
  3317. }
  3318. if (!dai->codec)
  3319. dai->dapm.idle_bias_off = 1;
  3320. list_add(&dai->list, &dai_list);
  3321. mutex_unlock(&client_mutex);
  3322. dev_dbg(dai->dev, "ASoC: Registered DAI '%s'\n", dai->name);
  3323. }
  3324. return 0;
  3325. err:
  3326. for (i--; i >= 0; i--)
  3327. snd_soc_unregister_dai(dev);
  3328. return ret;
  3329. }
  3330. /**
  3331. * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
  3332. *
  3333. * @dai: Array of DAIs to unregister
  3334. * @count: Number of DAIs
  3335. */
  3336. static void snd_soc_unregister_dais(struct device *dev, size_t count)
  3337. {
  3338. int i;
  3339. for (i = 0; i < count; i++)
  3340. snd_soc_unregister_dai(dev);
  3341. }
  3342. /**
  3343. * snd_soc_add_platform - Add a platform to the ASoC core
  3344. * @dev: The parent device for the platform
  3345. * @platform: The platform to add
  3346. * @platform_driver: The driver for the platform
  3347. */
  3348. int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
  3349. const struct snd_soc_platform_driver *platform_drv)
  3350. {
  3351. /* create platform component name */
  3352. platform->name = fmt_single_name(dev, &platform->id);
  3353. if (platform->name == NULL)
  3354. return -ENOMEM;
  3355. platform->dev = dev;
  3356. platform->driver = platform_drv;
  3357. platform->dapm.dev = dev;
  3358. platform->dapm.platform = platform;
  3359. platform->dapm.stream_event = platform_drv->stream_event;
  3360. mutex_init(&platform->mutex);
  3361. mutex_lock(&client_mutex);
  3362. list_add(&platform->list, &platform_list);
  3363. mutex_unlock(&client_mutex);
  3364. dev_dbg(dev, "ASoC: Registered platform '%s'\n", platform->name);
  3365. return 0;
  3366. }
  3367. EXPORT_SYMBOL_GPL(snd_soc_add_platform);
  3368. /**
  3369. * snd_soc_register_platform - Register a platform with the ASoC core
  3370. *
  3371. * @platform: platform to register
  3372. */
  3373. int snd_soc_register_platform(struct device *dev,
  3374. const struct snd_soc_platform_driver *platform_drv)
  3375. {
  3376. struct snd_soc_platform *platform;
  3377. int ret;
  3378. dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
  3379. platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
  3380. if (platform == NULL)
  3381. return -ENOMEM;
  3382. ret = snd_soc_add_platform(dev, platform, platform_drv);
  3383. if (ret)
  3384. kfree(platform);
  3385. return ret;
  3386. }
  3387. EXPORT_SYMBOL_GPL(snd_soc_register_platform);
  3388. /**
  3389. * snd_soc_remove_platform - Remove a platform from the ASoC core
  3390. * @platform: the platform to remove
  3391. */
  3392. void snd_soc_remove_platform(struct snd_soc_platform *platform)
  3393. {
  3394. mutex_lock(&client_mutex);
  3395. list_del(&platform->list);
  3396. mutex_unlock(&client_mutex);
  3397. dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
  3398. platform->name);
  3399. kfree(platform->name);
  3400. }
  3401. EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
  3402. struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
  3403. {
  3404. struct snd_soc_platform *platform;
  3405. list_for_each_entry(platform, &platform_list, list) {
  3406. if (dev == platform->dev)
  3407. return platform;
  3408. }
  3409. return NULL;
  3410. }
  3411. EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
  3412. /**
  3413. * snd_soc_unregister_platform - Unregister a platform from the ASoC core
  3414. *
  3415. * @platform: platform to unregister
  3416. */
  3417. void snd_soc_unregister_platform(struct device *dev)
  3418. {
  3419. struct snd_soc_platform *platform;
  3420. platform = snd_soc_lookup_platform(dev);
  3421. if (!platform)
  3422. return;
  3423. snd_soc_remove_platform(platform);
  3424. kfree(platform);
  3425. }
  3426. EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
  3427. static u64 codec_format_map[] = {
  3428. SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
  3429. SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
  3430. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
  3431. SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
  3432. SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
  3433. SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
  3434. SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
  3435. SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
  3436. SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
  3437. SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
  3438. SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
  3439. SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
  3440. SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
  3441. SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
  3442. SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
  3443. | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
  3444. };
  3445. /* Fix up the DAI formats for endianness: codecs don't actually see
  3446. * the endianness of the data but we're using the CPU format
  3447. * definitions which do need to include endianness so we ensure that
  3448. * codec DAIs always have both big and little endian variants set.
  3449. */
  3450. static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
  3451. {
  3452. int i;
  3453. for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
  3454. if (stream->formats & codec_format_map[i])
  3455. stream->formats |= codec_format_map[i];
  3456. }
  3457. /**
  3458. * snd_soc_register_codec - Register a codec with the ASoC core
  3459. *
  3460. * @codec: codec to register
  3461. */
  3462. int snd_soc_register_codec(struct device *dev,
  3463. const struct snd_soc_codec_driver *codec_drv,
  3464. struct snd_soc_dai_driver *dai_drv,
  3465. int num_dai)
  3466. {
  3467. size_t reg_size;
  3468. struct snd_soc_codec *codec;
  3469. int ret, i;
  3470. dev_dbg(dev, "codec register %s\n", dev_name(dev));
  3471. codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
  3472. if (codec == NULL)
  3473. return -ENOMEM;
  3474. /* create CODEC component name */
  3475. codec->name = fmt_single_name(dev, &codec->id);
  3476. if (codec->name == NULL) {
  3477. ret = -ENOMEM;
  3478. goto fail_codec;
  3479. }
  3480. if (codec_drv->compress_type)
  3481. codec->compress_type = codec_drv->compress_type;
  3482. else
  3483. codec->compress_type = SND_SOC_FLAT_COMPRESSION;
  3484. codec->write = codec_drv->write;
  3485. codec->read = codec_drv->read;
  3486. codec->volatile_register = codec_drv->volatile_register;
  3487. codec->readable_register = codec_drv->readable_register;
  3488. codec->writable_register = codec_drv->writable_register;
  3489. codec->ignore_pmdown_time = codec_drv->ignore_pmdown_time;
  3490. codec->dapm.bias_level = SND_SOC_BIAS_OFF;
  3491. codec->dapm.dev = dev;
  3492. codec->dapm.codec = codec;
  3493. codec->dapm.seq_notifier = codec_drv->seq_notifier;
  3494. codec->dapm.stream_event = codec_drv->stream_event;
  3495. codec->dev = dev;
  3496. codec->driver = codec_drv;
  3497. codec->num_dai = num_dai;
  3498. mutex_init(&codec->mutex);
  3499. /* allocate CODEC register cache */
  3500. if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
  3501. reg_size = codec_drv->reg_cache_size * codec_drv->reg_word_size;
  3502. codec->reg_size = reg_size;
  3503. /* it is necessary to make a copy of the default register cache
  3504. * because in the case of using a compression type that requires
  3505. * the default register cache to be marked as the
  3506. * kernel might have freed the array by the time we initialize
  3507. * the cache.
  3508. */
  3509. if (codec_drv->reg_cache_default) {
  3510. codec->reg_def_copy = kmemdup(codec_drv->reg_cache_default,
  3511. reg_size, GFP_KERNEL);
  3512. if (!codec->reg_def_copy) {
  3513. ret = -ENOMEM;
  3514. goto fail_codec_name;
  3515. }
  3516. }
  3517. }
  3518. if (codec_drv->reg_access_size && codec_drv->reg_access_default) {
  3519. if (!codec->volatile_register)
  3520. codec->volatile_register = snd_soc_default_volatile_register;
  3521. if (!codec->readable_register)
  3522. codec->readable_register = snd_soc_default_readable_register;
  3523. if (!codec->writable_register)
  3524. codec->writable_register = snd_soc_default_writable_register;
  3525. }
  3526. for (i = 0; i < num_dai; i++) {
  3527. fixup_codec_formats(&dai_drv[i].playback);
  3528. fixup_codec_formats(&dai_drv[i].capture);
  3529. }
  3530. mutex_lock(&client_mutex);
  3531. list_add(&codec->list, &codec_list);
  3532. mutex_unlock(&client_mutex);
  3533. /* register any DAIs */
  3534. ret = snd_soc_register_dais(dev, dai_drv, num_dai);
  3535. if (ret < 0) {
  3536. dev_err(codec->dev, "ASoC: Failed to regster DAIs: %d\n", ret);
  3537. goto fail_codec_name;
  3538. }
  3539. dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n", codec->name);
  3540. return 0;
  3541. fail_codec_name:
  3542. mutex_lock(&client_mutex);
  3543. list_del(&codec->list);
  3544. mutex_unlock(&client_mutex);
  3545. kfree(codec->name);
  3546. fail_codec:
  3547. kfree(codec);
  3548. return ret;
  3549. }
  3550. EXPORT_SYMBOL_GPL(snd_soc_register_codec);
  3551. /**
  3552. * snd_soc_unregister_codec - Unregister a codec from the ASoC core
  3553. *
  3554. * @codec: codec to unregister
  3555. */
  3556. void snd_soc_unregister_codec(struct device *dev)
  3557. {
  3558. struct snd_soc_codec *codec;
  3559. list_for_each_entry(codec, &codec_list, list) {
  3560. if (dev == codec->dev)
  3561. goto found;
  3562. }
  3563. return;
  3564. found:
  3565. snd_soc_unregister_dais(dev, codec->num_dai);
  3566. mutex_lock(&client_mutex);
  3567. list_del(&codec->list);
  3568. mutex_unlock(&client_mutex);
  3569. dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n", codec->name);
  3570. snd_soc_cache_exit(codec);
  3571. kfree(codec->reg_def_copy);
  3572. kfree(codec->name);
  3573. kfree(codec);
  3574. }
  3575. EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
  3576. /**
  3577. * snd_soc_register_component - Register a component with the ASoC core
  3578. *
  3579. */
  3580. int snd_soc_register_component(struct device *dev,
  3581. const struct snd_soc_component_driver *cmpnt_drv,
  3582. struct snd_soc_dai_driver *dai_drv,
  3583. int num_dai)
  3584. {
  3585. struct snd_soc_component *cmpnt;
  3586. int ret;
  3587. dev_dbg(dev, "component register %s\n", dev_name(dev));
  3588. cmpnt = devm_kzalloc(dev, sizeof(*cmpnt), GFP_KERNEL);
  3589. if (!cmpnt) {
  3590. dev_err(dev, "ASoC: Failed to allocate memory\n");
  3591. return -ENOMEM;
  3592. }
  3593. cmpnt->name = fmt_single_name(dev, &cmpnt->id);
  3594. if (!cmpnt->name) {
  3595. dev_err(dev, "ASoC: Failed to simplifying name\n");
  3596. return -ENOMEM;
  3597. }
  3598. cmpnt->dev = dev;
  3599. cmpnt->driver = cmpnt_drv;
  3600. cmpnt->num_dai = num_dai;
  3601. /*
  3602. * snd_soc_register_dai() uses fmt_single_name(), and
  3603. * snd_soc_register_dais() uses fmt_multiple_name()
  3604. * for dai->name which is used for name based matching
  3605. */
  3606. if (1 == num_dai)
  3607. ret = snd_soc_register_dai(dev, dai_drv);
  3608. else
  3609. ret = snd_soc_register_dais(dev, dai_drv, num_dai);
  3610. if (ret < 0) {
  3611. dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
  3612. goto error_component_name;
  3613. }
  3614. mutex_lock(&client_mutex);
  3615. list_add(&cmpnt->list, &component_list);
  3616. mutex_unlock(&client_mutex);
  3617. dev_dbg(cmpnt->dev, "ASoC: Registered component '%s'\n", cmpnt->name);
  3618. return ret;
  3619. error_component_name:
  3620. kfree(cmpnt->name);
  3621. return ret;
  3622. }
  3623. EXPORT_SYMBOL_GPL(snd_soc_register_component);
  3624. /**
  3625. * snd_soc_unregister_component - Unregister a component from the ASoC core
  3626. *
  3627. */
  3628. void snd_soc_unregister_component(struct device *dev)
  3629. {
  3630. struct snd_soc_component *cmpnt;
  3631. list_for_each_entry(cmpnt, &component_list, list) {
  3632. if (dev == cmpnt->dev)
  3633. goto found;
  3634. }
  3635. return;
  3636. found:
  3637. snd_soc_unregister_dais(dev, cmpnt->num_dai);
  3638. mutex_lock(&client_mutex);
  3639. list_del(&cmpnt->list);
  3640. mutex_unlock(&client_mutex);
  3641. dev_dbg(dev, "ASoC: Unregistered component '%s'\n", cmpnt->name);
  3642. kfree(cmpnt->name);
  3643. }
  3644. EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
  3645. /* Retrieve a card's name from device tree */
  3646. int snd_soc_of_parse_card_name(struct snd_soc_card *card,
  3647. const char *propname)
  3648. {
  3649. struct device_node *np = card->dev->of_node;
  3650. int ret;
  3651. ret = of_property_read_string_index(np, propname, 0, &card->name);
  3652. /*
  3653. * EINVAL means the property does not exist. This is fine providing
  3654. * card->name was previously set, which is checked later in
  3655. * snd_soc_register_card.
  3656. */
  3657. if (ret < 0 && ret != -EINVAL) {
  3658. dev_err(card->dev,
  3659. "ASoC: Property '%s' could not be read: %d\n",
  3660. propname, ret);
  3661. return ret;
  3662. }
  3663. return 0;
  3664. }
  3665. EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
  3666. int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
  3667. const char *propname)
  3668. {
  3669. struct device_node *np = card->dev->of_node;
  3670. int num_routes;
  3671. struct snd_soc_dapm_route *routes;
  3672. int i, ret;
  3673. num_routes = of_property_count_strings(np, propname);
  3674. if (num_routes < 0 || num_routes & 1) {
  3675. dev_err(card->dev,
  3676. "ASoC: Property '%s' does not exist or its length is not even\n",
  3677. propname);
  3678. return -EINVAL;
  3679. }
  3680. num_routes /= 2;
  3681. if (!num_routes) {
  3682. dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
  3683. propname);
  3684. return -EINVAL;
  3685. }
  3686. routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
  3687. GFP_KERNEL);
  3688. if (!routes) {
  3689. dev_err(card->dev,
  3690. "ASoC: Could not allocate DAPM route table\n");
  3691. return -EINVAL;
  3692. }
  3693. for (i = 0; i < num_routes; i++) {
  3694. ret = of_property_read_string_index(np, propname,
  3695. 2 * i, &routes[i].sink);
  3696. if (ret) {
  3697. dev_err(card->dev,
  3698. "ASoC: Property '%s' index %d could not be read: %d\n",
  3699. propname, 2 * i, ret);
  3700. return -EINVAL;
  3701. }
  3702. ret = of_property_read_string_index(np, propname,
  3703. (2 * i) + 1, &routes[i].source);
  3704. if (ret) {
  3705. dev_err(card->dev,
  3706. "ASoC: Property '%s' index %d could not be read: %d\n",
  3707. propname, (2 * i) + 1, ret);
  3708. return -EINVAL;
  3709. }
  3710. }
  3711. card->num_dapm_routes = num_routes;
  3712. card->dapm_routes = routes;
  3713. return 0;
  3714. }
  3715. EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
  3716. unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
  3717. const char *prefix)
  3718. {
  3719. int ret, i;
  3720. char prop[128];
  3721. unsigned int format = 0;
  3722. int bit, frame;
  3723. const char *str;
  3724. struct {
  3725. char *name;
  3726. unsigned int val;
  3727. } of_fmt_table[] = {
  3728. { "i2s", SND_SOC_DAIFMT_I2S },
  3729. { "right_j", SND_SOC_DAIFMT_RIGHT_J },
  3730. { "left_j", SND_SOC_DAIFMT_LEFT_J },
  3731. { "dsp_a", SND_SOC_DAIFMT_DSP_A },
  3732. { "dsp_b", SND_SOC_DAIFMT_DSP_B },
  3733. { "ac97", SND_SOC_DAIFMT_AC97 },
  3734. { "pdm", SND_SOC_DAIFMT_PDM},
  3735. { "msb", SND_SOC_DAIFMT_MSB },
  3736. { "lsb", SND_SOC_DAIFMT_LSB },
  3737. };
  3738. if (!prefix)
  3739. prefix = "";
  3740. /*
  3741. * check "[prefix]format = xxx"
  3742. * SND_SOC_DAIFMT_FORMAT_MASK area
  3743. */
  3744. snprintf(prop, sizeof(prop), "%sformat", prefix);
  3745. ret = of_property_read_string(np, prop, &str);
  3746. if (ret == 0) {
  3747. for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
  3748. if (strcmp(str, of_fmt_table[i].name) == 0) {
  3749. format |= of_fmt_table[i].val;
  3750. break;
  3751. }
  3752. }
  3753. }
  3754. /*
  3755. * check "[prefix]continuous-clock"
  3756. * SND_SOC_DAIFMT_CLOCK_MASK area
  3757. */
  3758. snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
  3759. if (of_get_property(np, prop, NULL))
  3760. format |= SND_SOC_DAIFMT_CONT;
  3761. else
  3762. format |= SND_SOC_DAIFMT_GATED;
  3763. /*
  3764. * check "[prefix]bitclock-inversion"
  3765. * check "[prefix]frame-inversion"
  3766. * SND_SOC_DAIFMT_INV_MASK area
  3767. */
  3768. snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
  3769. bit = !!of_get_property(np, prop, NULL);
  3770. snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
  3771. frame = !!of_get_property(np, prop, NULL);
  3772. switch ((bit << 4) + frame) {
  3773. case 0x11:
  3774. format |= SND_SOC_DAIFMT_IB_IF;
  3775. break;
  3776. case 0x10:
  3777. format |= SND_SOC_DAIFMT_IB_NF;
  3778. break;
  3779. case 0x01:
  3780. format |= SND_SOC_DAIFMT_NB_IF;
  3781. break;
  3782. default:
  3783. /* SND_SOC_DAIFMT_NB_NF is default */
  3784. break;
  3785. }
  3786. /*
  3787. * check "[prefix]bitclock-master"
  3788. * check "[prefix]frame-master"
  3789. * SND_SOC_DAIFMT_MASTER_MASK area
  3790. */
  3791. snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
  3792. bit = !!of_get_property(np, prop, NULL);
  3793. snprintf(prop, sizeof(prop), "%sframe-master", prefix);
  3794. frame = !!of_get_property(np, prop, NULL);
  3795. switch ((bit << 4) + frame) {
  3796. case 0x11:
  3797. format |= SND_SOC_DAIFMT_CBM_CFM;
  3798. break;
  3799. case 0x10:
  3800. format |= SND_SOC_DAIFMT_CBM_CFS;
  3801. break;
  3802. case 0x01:
  3803. format |= SND_SOC_DAIFMT_CBS_CFM;
  3804. break;
  3805. default:
  3806. format |= SND_SOC_DAIFMT_CBS_CFS;
  3807. break;
  3808. }
  3809. return format;
  3810. }
  3811. EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
  3812. static int __init snd_soc_init(void)
  3813. {
  3814. #ifdef CONFIG_DEBUG_FS
  3815. snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
  3816. if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
  3817. pr_warn("ASoC: Failed to create debugfs directory\n");
  3818. snd_soc_debugfs_root = NULL;
  3819. }
  3820. if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
  3821. &codec_list_fops))
  3822. pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
  3823. if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
  3824. &dai_list_fops))
  3825. pr_warn("ASoC: Failed to create DAI list debugfs file\n");
  3826. if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
  3827. &platform_list_fops))
  3828. pr_warn("ASoC: Failed to create platform list debugfs file\n");
  3829. #endif
  3830. snd_soc_util_init();
  3831. return platform_driver_register(&soc_driver);
  3832. }
  3833. module_init(snd_soc_init);
  3834. static void __exit snd_soc_exit(void)
  3835. {
  3836. snd_soc_util_exit();
  3837. #ifdef CONFIG_DEBUG_FS
  3838. debugfs_remove_recursive(snd_soc_debugfs_root);
  3839. #endif
  3840. platform_driver_unregister(&soc_driver);
  3841. }
  3842. module_exit(snd_soc_exit);
  3843. /* Module information */
  3844. MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
  3845. MODULE_DESCRIPTION("ALSA SoC Core");
  3846. MODULE_LICENSE("GPL");
  3847. MODULE_ALIAS("platform:soc-audio");