soc-core.c 86 KB

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