soc-core.c 122 KB

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