soc-core.c 107 KB

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