soc-core.c 105 KB

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