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