soc-core.c 98 KB

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