soc-core.c 117 KB

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