soc-core.c 95 KB

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