soc-core.c 86 KB

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