soc-core.c 101 KB

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