soc-core.c 112 KB

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