soc-core.c 116 KB

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