soc-core.c 88 KB

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