soc-core.c 90 KB

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