soc-core.c 100 KB

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