soc-core.c 101 KB

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