soc-core.c 114 KB

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