soc-core.c 105 KB

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