soc-core.c 100 KB

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