soc-core.c 88 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510
  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. SET_SYSTEM_SLEEP_PM_OPS(snd_soc_suspend, snd_soc_resume)
  1379. .poweroff = snd_soc_poweroff,
  1380. };
  1381. EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
  1382. /* ASoC platform driver */
  1383. static struct platform_driver soc_driver = {
  1384. .driver = {
  1385. .name = "soc-audio",
  1386. .owner = THIS_MODULE,
  1387. .pm = &snd_soc_pm_ops,
  1388. },
  1389. .probe = soc_probe,
  1390. .remove = soc_remove,
  1391. };
  1392. /**
  1393. * snd_soc_codec_volatile_register: Report if a register is volatile.
  1394. *
  1395. * @codec: CODEC to query.
  1396. * @reg: Register to query.
  1397. *
  1398. * Boolean function indiciating if a CODEC register is volatile.
  1399. */
  1400. int snd_soc_codec_volatile_register(struct snd_soc_codec *codec,
  1401. unsigned int reg)
  1402. {
  1403. if (codec->volatile_register)
  1404. return codec->volatile_register(codec, reg);
  1405. else
  1406. return 0;
  1407. }
  1408. EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
  1409. /**
  1410. * snd_soc_codec_readable_register: Report if a register is readable.
  1411. *
  1412. * @codec: CODEC to query.
  1413. * @reg: Register to query.
  1414. *
  1415. * Boolean function indicating if a CODEC register is readable.
  1416. */
  1417. int snd_soc_codec_readable_register(struct snd_soc_codec *codec,
  1418. unsigned int reg)
  1419. {
  1420. if (codec->readable_register)
  1421. return codec->readable_register(codec, reg);
  1422. else
  1423. return 1;
  1424. }
  1425. EXPORT_SYMBOL_GPL(snd_soc_codec_readable_register);
  1426. /**
  1427. * snd_soc_codec_writable_register: Report if a register is writable.
  1428. *
  1429. * @codec: CODEC to query.
  1430. * @reg: Register to query.
  1431. *
  1432. * Boolean function indicating if a CODEC register is writable.
  1433. */
  1434. int snd_soc_codec_writable_register(struct snd_soc_codec *codec,
  1435. unsigned int reg)
  1436. {
  1437. if (codec->writable_register)
  1438. return codec->writable_register(codec, reg);
  1439. else
  1440. return 1;
  1441. }
  1442. EXPORT_SYMBOL_GPL(snd_soc_codec_writable_register);
  1443. int snd_soc_platform_read(struct snd_soc_platform *platform,
  1444. unsigned int reg)
  1445. {
  1446. unsigned int ret;
  1447. if (!platform->driver->read) {
  1448. dev_err(platform->dev, "platform has no read back\n");
  1449. return -1;
  1450. }
  1451. ret = platform->driver->read(platform, reg);
  1452. dev_dbg(platform->dev, "read %x => %x\n", reg, ret);
  1453. trace_snd_soc_preg_read(platform, reg, ret);
  1454. return ret;
  1455. }
  1456. EXPORT_SYMBOL_GPL(snd_soc_platform_read);
  1457. int snd_soc_platform_write(struct snd_soc_platform *platform,
  1458. unsigned int reg, unsigned int val)
  1459. {
  1460. if (!platform->driver->write) {
  1461. dev_err(platform->dev, "platform has no write back\n");
  1462. return -1;
  1463. }
  1464. dev_dbg(platform->dev, "write %x = %x\n", reg, val);
  1465. trace_snd_soc_preg_write(platform, reg, val);
  1466. return platform->driver->write(platform, reg, val);
  1467. }
  1468. EXPORT_SYMBOL_GPL(snd_soc_platform_write);
  1469. /**
  1470. * snd_soc_new_ac97_codec - initailise AC97 device
  1471. * @codec: audio codec
  1472. * @ops: AC97 bus operations
  1473. * @num: AC97 codec number
  1474. *
  1475. * Initialises AC97 codec resources for use by ad-hoc devices only.
  1476. */
  1477. int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
  1478. struct snd_ac97_bus_ops *ops, int num)
  1479. {
  1480. mutex_lock(&codec->mutex);
  1481. codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
  1482. if (codec->ac97 == NULL) {
  1483. mutex_unlock(&codec->mutex);
  1484. return -ENOMEM;
  1485. }
  1486. codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
  1487. if (codec->ac97->bus == NULL) {
  1488. kfree(codec->ac97);
  1489. codec->ac97 = NULL;
  1490. mutex_unlock(&codec->mutex);
  1491. return -ENOMEM;
  1492. }
  1493. codec->ac97->bus->ops = ops;
  1494. codec->ac97->num = num;
  1495. /*
  1496. * Mark the AC97 device to be created by us. This way we ensure that the
  1497. * device will be registered with the device subsystem later on.
  1498. */
  1499. codec->ac97_created = 1;
  1500. mutex_unlock(&codec->mutex);
  1501. return 0;
  1502. }
  1503. EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
  1504. /**
  1505. * snd_soc_free_ac97_codec - free AC97 codec device
  1506. * @codec: audio codec
  1507. *
  1508. * Frees AC97 codec device resources.
  1509. */
  1510. void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
  1511. {
  1512. mutex_lock(&codec->mutex);
  1513. #ifdef CONFIG_SND_SOC_AC97_BUS
  1514. soc_unregister_ac97_dai_link(codec);
  1515. #endif
  1516. kfree(codec->ac97->bus);
  1517. kfree(codec->ac97);
  1518. codec->ac97 = NULL;
  1519. codec->ac97_created = 0;
  1520. mutex_unlock(&codec->mutex);
  1521. }
  1522. EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
  1523. unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
  1524. {
  1525. unsigned int ret;
  1526. ret = codec->read(codec, reg);
  1527. dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
  1528. trace_snd_soc_reg_read(codec, reg, ret);
  1529. return ret;
  1530. }
  1531. EXPORT_SYMBOL_GPL(snd_soc_read);
  1532. unsigned int snd_soc_write(struct snd_soc_codec *codec,
  1533. unsigned int reg, unsigned int val)
  1534. {
  1535. dev_dbg(codec->dev, "write %x = %x\n", reg, val);
  1536. trace_snd_soc_reg_write(codec, reg, val);
  1537. return codec->write(codec, reg, val);
  1538. }
  1539. EXPORT_SYMBOL_GPL(snd_soc_write);
  1540. unsigned int snd_soc_bulk_write_raw(struct snd_soc_codec *codec,
  1541. unsigned int reg, const void *data, size_t len)
  1542. {
  1543. return codec->bulk_write_raw(codec, reg, data, len);
  1544. }
  1545. EXPORT_SYMBOL_GPL(snd_soc_bulk_write_raw);
  1546. /**
  1547. * snd_soc_update_bits - update codec register bits
  1548. * @codec: audio codec
  1549. * @reg: codec register
  1550. * @mask: register mask
  1551. * @value: new value
  1552. *
  1553. * Writes new register value.
  1554. *
  1555. * Returns 1 for change, 0 for no change, or negative error code.
  1556. */
  1557. int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
  1558. unsigned int mask, unsigned int value)
  1559. {
  1560. bool change;
  1561. unsigned int old, new;
  1562. int ret;
  1563. if (codec->using_regmap) {
  1564. ret = regmap_update_bits_check(codec->control_data, reg,
  1565. mask, value, &change);
  1566. } else {
  1567. ret = snd_soc_read(codec, reg);
  1568. if (ret < 0)
  1569. return ret;
  1570. old = ret;
  1571. new = (old & ~mask) | (value & mask);
  1572. change = old != new;
  1573. if (change)
  1574. ret = snd_soc_write(codec, reg, new);
  1575. }
  1576. if (ret < 0)
  1577. return ret;
  1578. return change;
  1579. }
  1580. EXPORT_SYMBOL_GPL(snd_soc_update_bits);
  1581. /**
  1582. * snd_soc_update_bits_locked - update codec register bits
  1583. * @codec: audio codec
  1584. * @reg: codec register
  1585. * @mask: register mask
  1586. * @value: new value
  1587. *
  1588. * Writes new register value, and takes the codec mutex.
  1589. *
  1590. * Returns 1 for change else 0.
  1591. */
  1592. int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
  1593. unsigned short reg, unsigned int mask,
  1594. unsigned int value)
  1595. {
  1596. int change;
  1597. mutex_lock(&codec->mutex);
  1598. change = snd_soc_update_bits(codec, reg, mask, value);
  1599. mutex_unlock(&codec->mutex);
  1600. return change;
  1601. }
  1602. EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
  1603. /**
  1604. * snd_soc_test_bits - test register for change
  1605. * @codec: audio codec
  1606. * @reg: codec register
  1607. * @mask: register mask
  1608. * @value: new value
  1609. *
  1610. * Tests a register with a new value and checks if the new value is
  1611. * different from the old value.
  1612. *
  1613. * Returns 1 for change else 0.
  1614. */
  1615. int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
  1616. unsigned int mask, unsigned int value)
  1617. {
  1618. int change;
  1619. unsigned int old, new;
  1620. old = snd_soc_read(codec, reg);
  1621. new = (old & ~mask) | value;
  1622. change = old != new;
  1623. return change;
  1624. }
  1625. EXPORT_SYMBOL_GPL(snd_soc_test_bits);
  1626. /**
  1627. * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
  1628. * @substream: the pcm substream
  1629. * @hw: the hardware parameters
  1630. *
  1631. * Sets the substream runtime hardware parameters.
  1632. */
  1633. int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
  1634. const struct snd_pcm_hardware *hw)
  1635. {
  1636. struct snd_pcm_runtime *runtime = substream->runtime;
  1637. runtime->hw.info = hw->info;
  1638. runtime->hw.formats = hw->formats;
  1639. runtime->hw.period_bytes_min = hw->period_bytes_min;
  1640. runtime->hw.period_bytes_max = hw->period_bytes_max;
  1641. runtime->hw.periods_min = hw->periods_min;
  1642. runtime->hw.periods_max = hw->periods_max;
  1643. runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
  1644. runtime->hw.fifo_size = hw->fifo_size;
  1645. return 0;
  1646. }
  1647. EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
  1648. /**
  1649. * snd_soc_cnew - create new control
  1650. * @_template: control template
  1651. * @data: control private data
  1652. * @long_name: control long name
  1653. * @prefix: control name prefix
  1654. *
  1655. * Create a new mixer control from a template control.
  1656. *
  1657. * Returns 0 for success, else error.
  1658. */
  1659. struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
  1660. void *data, char *long_name,
  1661. const char *prefix)
  1662. {
  1663. struct snd_kcontrol_new template;
  1664. struct snd_kcontrol *kcontrol;
  1665. char *name = NULL;
  1666. int name_len;
  1667. memcpy(&template, _template, sizeof(template));
  1668. template.index = 0;
  1669. if (!long_name)
  1670. long_name = template.name;
  1671. if (prefix) {
  1672. name_len = strlen(long_name) + strlen(prefix) + 2;
  1673. name = kmalloc(name_len, GFP_KERNEL);
  1674. if (!name)
  1675. return NULL;
  1676. snprintf(name, name_len, "%s %s", prefix, long_name);
  1677. template.name = name;
  1678. } else {
  1679. template.name = long_name;
  1680. }
  1681. kcontrol = snd_ctl_new1(&template, data);
  1682. kfree(name);
  1683. return kcontrol;
  1684. }
  1685. EXPORT_SYMBOL_GPL(snd_soc_cnew);
  1686. /**
  1687. * snd_soc_add_controls - add an array of controls to a codec.
  1688. * Convienience function to add a list of controls. Many codecs were
  1689. * duplicating this code.
  1690. *
  1691. * @codec: codec to add controls to
  1692. * @controls: array of controls to add
  1693. * @num_controls: number of elements in the array
  1694. *
  1695. * Return 0 for success, else error.
  1696. */
  1697. int snd_soc_add_controls(struct snd_soc_codec *codec,
  1698. const struct snd_kcontrol_new *controls, int num_controls)
  1699. {
  1700. struct snd_card *card = codec->card->snd_card;
  1701. int err, i;
  1702. for (i = 0; i < num_controls; i++) {
  1703. const struct snd_kcontrol_new *control = &controls[i];
  1704. err = snd_ctl_add(card, snd_soc_cnew(control, codec,
  1705. control->name,
  1706. codec->name_prefix));
  1707. if (err < 0) {
  1708. dev_err(codec->dev, "%s: Failed to add %s: %d\n",
  1709. codec->name, control->name, err);
  1710. return err;
  1711. }
  1712. }
  1713. return 0;
  1714. }
  1715. EXPORT_SYMBOL_GPL(snd_soc_add_controls);
  1716. /**
  1717. * snd_soc_add_platform_controls - add an array of controls to a platform.
  1718. * Convienience function to add a list of controls.
  1719. *
  1720. * @platform: platform to add controls to
  1721. * @controls: array of controls to add
  1722. * @num_controls: number of elements in the array
  1723. *
  1724. * Return 0 for success, else error.
  1725. */
  1726. int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
  1727. const struct snd_kcontrol_new *controls, int num_controls)
  1728. {
  1729. struct snd_card *card = platform->card->snd_card;
  1730. int err, i;
  1731. for (i = 0; i < num_controls; i++) {
  1732. const struct snd_kcontrol_new *control = &controls[i];
  1733. err = snd_ctl_add(card, snd_soc_cnew(control, platform,
  1734. control->name, NULL));
  1735. if (err < 0) {
  1736. dev_err(platform->dev, "Failed to add %s %d\n",control->name, err);
  1737. return err;
  1738. }
  1739. }
  1740. return 0;
  1741. }
  1742. EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
  1743. /**
  1744. * snd_soc_info_enum_double - enumerated double mixer info callback
  1745. * @kcontrol: mixer control
  1746. * @uinfo: control element information
  1747. *
  1748. * Callback to provide information about a double enumerated
  1749. * mixer control.
  1750. *
  1751. * Returns 0 for success.
  1752. */
  1753. int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
  1754. struct snd_ctl_elem_info *uinfo)
  1755. {
  1756. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1757. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  1758. uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
  1759. uinfo->value.enumerated.items = e->max;
  1760. if (uinfo->value.enumerated.item > e->max - 1)
  1761. uinfo->value.enumerated.item = e->max - 1;
  1762. strcpy(uinfo->value.enumerated.name,
  1763. e->texts[uinfo->value.enumerated.item]);
  1764. return 0;
  1765. }
  1766. EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
  1767. /**
  1768. * snd_soc_get_enum_double - enumerated double mixer get callback
  1769. * @kcontrol: mixer control
  1770. * @ucontrol: control element information
  1771. *
  1772. * Callback to get the value of a double enumerated mixer.
  1773. *
  1774. * Returns 0 for success.
  1775. */
  1776. int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
  1777. struct snd_ctl_elem_value *ucontrol)
  1778. {
  1779. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1780. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1781. unsigned int val, bitmask;
  1782. for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
  1783. ;
  1784. val = snd_soc_read(codec, e->reg);
  1785. ucontrol->value.enumerated.item[0]
  1786. = (val >> e->shift_l) & (bitmask - 1);
  1787. if (e->shift_l != e->shift_r)
  1788. ucontrol->value.enumerated.item[1] =
  1789. (val >> e->shift_r) & (bitmask - 1);
  1790. return 0;
  1791. }
  1792. EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
  1793. /**
  1794. * snd_soc_put_enum_double - enumerated double mixer put callback
  1795. * @kcontrol: mixer control
  1796. * @ucontrol: control element information
  1797. *
  1798. * Callback to set the value of a double enumerated mixer.
  1799. *
  1800. * Returns 0 for success.
  1801. */
  1802. int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
  1803. struct snd_ctl_elem_value *ucontrol)
  1804. {
  1805. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1806. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1807. unsigned int val;
  1808. unsigned int mask, bitmask;
  1809. for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
  1810. ;
  1811. if (ucontrol->value.enumerated.item[0] > e->max - 1)
  1812. return -EINVAL;
  1813. val = ucontrol->value.enumerated.item[0] << e->shift_l;
  1814. mask = (bitmask - 1) << e->shift_l;
  1815. if (e->shift_l != e->shift_r) {
  1816. if (ucontrol->value.enumerated.item[1] > e->max - 1)
  1817. return -EINVAL;
  1818. val |= ucontrol->value.enumerated.item[1] << e->shift_r;
  1819. mask |= (bitmask - 1) << e->shift_r;
  1820. }
  1821. return snd_soc_update_bits_locked(codec, e->reg, mask, val);
  1822. }
  1823. EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
  1824. /**
  1825. * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
  1826. * @kcontrol: mixer control
  1827. * @ucontrol: control element information
  1828. *
  1829. * Callback to get the value of a double semi enumerated mixer.
  1830. *
  1831. * Semi enumerated mixer: the enumerated items are referred as values. Can be
  1832. * used for handling bitfield coded enumeration for example.
  1833. *
  1834. * Returns 0 for success.
  1835. */
  1836. int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
  1837. struct snd_ctl_elem_value *ucontrol)
  1838. {
  1839. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1840. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1841. unsigned int reg_val, val, mux;
  1842. reg_val = snd_soc_read(codec, e->reg);
  1843. val = (reg_val >> e->shift_l) & e->mask;
  1844. for (mux = 0; mux < e->max; mux++) {
  1845. if (val == e->values[mux])
  1846. break;
  1847. }
  1848. ucontrol->value.enumerated.item[0] = mux;
  1849. if (e->shift_l != e->shift_r) {
  1850. val = (reg_val >> e->shift_r) & e->mask;
  1851. for (mux = 0; mux < e->max; mux++) {
  1852. if (val == e->values[mux])
  1853. break;
  1854. }
  1855. ucontrol->value.enumerated.item[1] = mux;
  1856. }
  1857. return 0;
  1858. }
  1859. EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
  1860. /**
  1861. * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
  1862. * @kcontrol: mixer control
  1863. * @ucontrol: control element information
  1864. *
  1865. * Callback to set the value of a double semi enumerated mixer.
  1866. *
  1867. * Semi enumerated mixer: the enumerated items are referred as values. Can be
  1868. * used for handling bitfield coded enumeration for example.
  1869. *
  1870. * Returns 0 for success.
  1871. */
  1872. int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
  1873. struct snd_ctl_elem_value *ucontrol)
  1874. {
  1875. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1876. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1877. unsigned int val;
  1878. unsigned int mask;
  1879. if (ucontrol->value.enumerated.item[0] > e->max - 1)
  1880. return -EINVAL;
  1881. val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
  1882. mask = e->mask << e->shift_l;
  1883. if (e->shift_l != e->shift_r) {
  1884. if (ucontrol->value.enumerated.item[1] > e->max - 1)
  1885. return -EINVAL;
  1886. val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
  1887. mask |= e->mask << e->shift_r;
  1888. }
  1889. return snd_soc_update_bits_locked(codec, e->reg, mask, val);
  1890. }
  1891. EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
  1892. /**
  1893. * snd_soc_info_enum_ext - external enumerated single mixer info callback
  1894. * @kcontrol: mixer control
  1895. * @uinfo: control element information
  1896. *
  1897. * Callback to provide information about an external enumerated
  1898. * single mixer.
  1899. *
  1900. * Returns 0 for success.
  1901. */
  1902. int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
  1903. struct snd_ctl_elem_info *uinfo)
  1904. {
  1905. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1906. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  1907. uinfo->count = 1;
  1908. uinfo->value.enumerated.items = e->max;
  1909. if (uinfo->value.enumerated.item > e->max - 1)
  1910. uinfo->value.enumerated.item = e->max - 1;
  1911. strcpy(uinfo->value.enumerated.name,
  1912. e->texts[uinfo->value.enumerated.item]);
  1913. return 0;
  1914. }
  1915. EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
  1916. /**
  1917. * snd_soc_info_volsw_ext - external single mixer info callback
  1918. * @kcontrol: mixer control
  1919. * @uinfo: control element information
  1920. *
  1921. * Callback to provide information about a single external mixer control.
  1922. *
  1923. * Returns 0 for success.
  1924. */
  1925. int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
  1926. struct snd_ctl_elem_info *uinfo)
  1927. {
  1928. int max = kcontrol->private_value;
  1929. if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
  1930. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  1931. else
  1932. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1933. uinfo->count = 1;
  1934. uinfo->value.integer.min = 0;
  1935. uinfo->value.integer.max = max;
  1936. return 0;
  1937. }
  1938. EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
  1939. /**
  1940. * snd_soc_info_volsw - single mixer info callback
  1941. * @kcontrol: mixer control
  1942. * @uinfo: control element information
  1943. *
  1944. * Callback to provide information about a single mixer control, or a double
  1945. * mixer control that spans 2 registers.
  1946. *
  1947. * Returns 0 for success.
  1948. */
  1949. int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
  1950. struct snd_ctl_elem_info *uinfo)
  1951. {
  1952. struct soc_mixer_control *mc =
  1953. (struct soc_mixer_control *)kcontrol->private_value;
  1954. int platform_max;
  1955. if (!mc->platform_max)
  1956. mc->platform_max = mc->max;
  1957. platform_max = mc->platform_max;
  1958. if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
  1959. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  1960. else
  1961. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1962. uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
  1963. uinfo->value.integer.min = 0;
  1964. uinfo->value.integer.max = platform_max;
  1965. return 0;
  1966. }
  1967. EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
  1968. /**
  1969. * snd_soc_get_volsw - single mixer get callback
  1970. * @kcontrol: mixer control
  1971. * @ucontrol: control element information
  1972. *
  1973. * Callback to get the value of a single mixer control, or a double mixer
  1974. * control that spans 2 registers.
  1975. *
  1976. * Returns 0 for success.
  1977. */
  1978. int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
  1979. struct snd_ctl_elem_value *ucontrol)
  1980. {
  1981. struct soc_mixer_control *mc =
  1982. (struct soc_mixer_control *)kcontrol->private_value;
  1983. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1984. unsigned int reg = mc->reg;
  1985. unsigned int reg2 = mc->rreg;
  1986. unsigned int shift = mc->shift;
  1987. unsigned int rshift = mc->rshift;
  1988. int max = mc->max;
  1989. unsigned int mask = (1 << fls(max)) - 1;
  1990. unsigned int invert = mc->invert;
  1991. ucontrol->value.integer.value[0] =
  1992. (snd_soc_read(codec, reg) >> shift) & mask;
  1993. if (invert)
  1994. ucontrol->value.integer.value[0] =
  1995. max - ucontrol->value.integer.value[0];
  1996. if (snd_soc_volsw_is_stereo(mc)) {
  1997. if (reg == reg2)
  1998. ucontrol->value.integer.value[1] =
  1999. (snd_soc_read(codec, reg) >> rshift) & mask;
  2000. else
  2001. ucontrol->value.integer.value[1] =
  2002. (snd_soc_read(codec, reg2) >> shift) & mask;
  2003. if (invert)
  2004. ucontrol->value.integer.value[1] =
  2005. max - ucontrol->value.integer.value[1];
  2006. }
  2007. return 0;
  2008. }
  2009. EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
  2010. /**
  2011. * snd_soc_put_volsw - single mixer put callback
  2012. * @kcontrol: mixer control
  2013. * @ucontrol: control element information
  2014. *
  2015. * Callback to set the value of a single mixer control, or a double mixer
  2016. * control that spans 2 registers.
  2017. *
  2018. * Returns 0 for success.
  2019. */
  2020. int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
  2021. struct snd_ctl_elem_value *ucontrol)
  2022. {
  2023. struct soc_mixer_control *mc =
  2024. (struct soc_mixer_control *)kcontrol->private_value;
  2025. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2026. unsigned int reg = mc->reg;
  2027. unsigned int reg2 = mc->rreg;
  2028. unsigned int shift = mc->shift;
  2029. unsigned int rshift = mc->rshift;
  2030. int max = mc->max;
  2031. unsigned int mask = (1 << fls(max)) - 1;
  2032. unsigned int invert = mc->invert;
  2033. int err;
  2034. bool type_2r = 0;
  2035. unsigned int val2 = 0;
  2036. unsigned int val, val_mask;
  2037. val = (ucontrol->value.integer.value[0] & mask);
  2038. if (invert)
  2039. val = max - val;
  2040. val_mask = mask << shift;
  2041. val = val << shift;
  2042. if (snd_soc_volsw_is_stereo(mc)) {
  2043. val2 = (ucontrol->value.integer.value[1] & mask);
  2044. if (invert)
  2045. val2 = max - val2;
  2046. if (reg == reg2) {
  2047. val_mask |= mask << rshift;
  2048. val |= val2 << rshift;
  2049. } else {
  2050. val2 = val2 << shift;
  2051. type_2r = 1;
  2052. }
  2053. }
  2054. err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
  2055. if (err < 0)
  2056. return err;
  2057. if (type_2r)
  2058. err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
  2059. return err;
  2060. }
  2061. EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
  2062. /**
  2063. * snd_soc_info_volsw_s8 - signed mixer info callback
  2064. * @kcontrol: mixer control
  2065. * @uinfo: control element information
  2066. *
  2067. * Callback to provide information about a signed mixer control.
  2068. *
  2069. * Returns 0 for success.
  2070. */
  2071. int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
  2072. struct snd_ctl_elem_info *uinfo)
  2073. {
  2074. struct soc_mixer_control *mc =
  2075. (struct soc_mixer_control *)kcontrol->private_value;
  2076. int platform_max;
  2077. int min = mc->min;
  2078. if (!mc->platform_max)
  2079. mc->platform_max = mc->max;
  2080. platform_max = mc->platform_max;
  2081. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2082. uinfo->count = 2;
  2083. uinfo->value.integer.min = 0;
  2084. uinfo->value.integer.max = platform_max - min;
  2085. return 0;
  2086. }
  2087. EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
  2088. /**
  2089. * snd_soc_get_volsw_s8 - signed mixer get callback
  2090. * @kcontrol: mixer control
  2091. * @ucontrol: control element information
  2092. *
  2093. * Callback to get the value of a signed mixer control.
  2094. *
  2095. * Returns 0 for success.
  2096. */
  2097. int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
  2098. struct snd_ctl_elem_value *ucontrol)
  2099. {
  2100. struct soc_mixer_control *mc =
  2101. (struct soc_mixer_control *)kcontrol->private_value;
  2102. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2103. unsigned int reg = mc->reg;
  2104. int min = mc->min;
  2105. int val = snd_soc_read(codec, reg);
  2106. ucontrol->value.integer.value[0] =
  2107. ((signed char)(val & 0xff))-min;
  2108. ucontrol->value.integer.value[1] =
  2109. ((signed char)((val >> 8) & 0xff))-min;
  2110. return 0;
  2111. }
  2112. EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
  2113. /**
  2114. * snd_soc_put_volsw_sgn - signed mixer put callback
  2115. * @kcontrol: mixer control
  2116. * @ucontrol: control element information
  2117. *
  2118. * Callback to set the value of a signed mixer control.
  2119. *
  2120. * Returns 0 for success.
  2121. */
  2122. int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
  2123. struct snd_ctl_elem_value *ucontrol)
  2124. {
  2125. struct soc_mixer_control *mc =
  2126. (struct soc_mixer_control *)kcontrol->private_value;
  2127. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2128. unsigned int reg = mc->reg;
  2129. int min = mc->min;
  2130. unsigned int val;
  2131. val = (ucontrol->value.integer.value[0]+min) & 0xff;
  2132. val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
  2133. return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
  2134. }
  2135. EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
  2136. /**
  2137. * snd_soc_limit_volume - Set new limit to an existing volume control.
  2138. *
  2139. * @codec: where to look for the control
  2140. * @name: Name of the control
  2141. * @max: new maximum limit
  2142. *
  2143. * Return 0 for success, else error.
  2144. */
  2145. int snd_soc_limit_volume(struct snd_soc_codec *codec,
  2146. const char *name, int max)
  2147. {
  2148. struct snd_card *card = codec->card->snd_card;
  2149. struct snd_kcontrol *kctl;
  2150. struct soc_mixer_control *mc;
  2151. int found = 0;
  2152. int ret = -EINVAL;
  2153. /* Sanity check for name and max */
  2154. if (unlikely(!name || max <= 0))
  2155. return -EINVAL;
  2156. list_for_each_entry(kctl, &card->controls, list) {
  2157. if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
  2158. found = 1;
  2159. break;
  2160. }
  2161. }
  2162. if (found) {
  2163. mc = (struct soc_mixer_control *)kctl->private_value;
  2164. if (max <= mc->max) {
  2165. mc->platform_max = max;
  2166. ret = 0;
  2167. }
  2168. }
  2169. return ret;
  2170. }
  2171. EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
  2172. /**
  2173. * snd_soc_info_volsw_2r_sx - double with tlv and variable data size
  2174. * mixer info callback
  2175. * @kcontrol: mixer control
  2176. * @uinfo: control element information
  2177. *
  2178. * Returns 0 for success.
  2179. */
  2180. int snd_soc_info_volsw_2r_sx(struct snd_kcontrol *kcontrol,
  2181. struct snd_ctl_elem_info *uinfo)
  2182. {
  2183. struct soc_mixer_control *mc =
  2184. (struct soc_mixer_control *)kcontrol->private_value;
  2185. int max = mc->max;
  2186. int min = mc->min;
  2187. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2188. uinfo->count = 2;
  2189. uinfo->value.integer.min = 0;
  2190. uinfo->value.integer.max = max-min;
  2191. return 0;
  2192. }
  2193. EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r_sx);
  2194. /**
  2195. * snd_soc_get_volsw_2r_sx - double with tlv and variable data size
  2196. * mixer get callback
  2197. * @kcontrol: mixer control
  2198. * @uinfo: control element information
  2199. *
  2200. * Returns 0 for success.
  2201. */
  2202. int snd_soc_get_volsw_2r_sx(struct snd_kcontrol *kcontrol,
  2203. struct snd_ctl_elem_value *ucontrol)
  2204. {
  2205. struct soc_mixer_control *mc =
  2206. (struct soc_mixer_control *)kcontrol->private_value;
  2207. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2208. unsigned int mask = (1<<mc->shift)-1;
  2209. int min = mc->min;
  2210. int val = snd_soc_read(codec, mc->reg) & mask;
  2211. int valr = snd_soc_read(codec, mc->rreg) & mask;
  2212. ucontrol->value.integer.value[0] = ((val & 0xff)-min) & mask;
  2213. ucontrol->value.integer.value[1] = ((valr & 0xff)-min) & mask;
  2214. return 0;
  2215. }
  2216. EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r_sx);
  2217. /**
  2218. * snd_soc_put_volsw_2r_sx - double with tlv and variable data size
  2219. * mixer put callback
  2220. * @kcontrol: mixer control
  2221. * @uinfo: control element information
  2222. *
  2223. * Returns 0 for success.
  2224. */
  2225. int snd_soc_put_volsw_2r_sx(struct snd_kcontrol *kcontrol,
  2226. struct snd_ctl_elem_value *ucontrol)
  2227. {
  2228. struct soc_mixer_control *mc =
  2229. (struct soc_mixer_control *)kcontrol->private_value;
  2230. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2231. unsigned int mask = (1<<mc->shift)-1;
  2232. int min = mc->min;
  2233. int ret;
  2234. unsigned int val, valr, oval, ovalr;
  2235. val = ((ucontrol->value.integer.value[0]+min) & 0xff);
  2236. val &= mask;
  2237. valr = ((ucontrol->value.integer.value[1]+min) & 0xff);
  2238. valr &= mask;
  2239. oval = snd_soc_read(codec, mc->reg) & mask;
  2240. ovalr = snd_soc_read(codec, mc->rreg) & mask;
  2241. ret = 0;
  2242. if (oval != val) {
  2243. ret = snd_soc_write(codec, mc->reg, val);
  2244. if (ret < 0)
  2245. return ret;
  2246. }
  2247. if (ovalr != valr) {
  2248. ret = snd_soc_write(codec, mc->rreg, valr);
  2249. if (ret < 0)
  2250. return ret;
  2251. }
  2252. return 0;
  2253. }
  2254. EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r_sx);
  2255. /**
  2256. * snd_soc_dai_set_sysclk - configure DAI system or master clock.
  2257. * @dai: DAI
  2258. * @clk_id: DAI specific clock ID
  2259. * @freq: new clock frequency in Hz
  2260. * @dir: new clock direction - input/output.
  2261. *
  2262. * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
  2263. */
  2264. int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
  2265. unsigned int freq, int dir)
  2266. {
  2267. if (dai->driver && dai->driver->ops->set_sysclk)
  2268. return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
  2269. else if (dai->codec && dai->codec->driver->set_sysclk)
  2270. return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
  2271. freq, dir);
  2272. else
  2273. return -EINVAL;
  2274. }
  2275. EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
  2276. /**
  2277. * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
  2278. * @codec: CODEC
  2279. * @clk_id: DAI specific clock ID
  2280. * @source: Source for the clock
  2281. * @freq: new clock frequency in Hz
  2282. * @dir: new clock direction - input/output.
  2283. *
  2284. * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
  2285. */
  2286. int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
  2287. int source, unsigned int freq, int dir)
  2288. {
  2289. if (codec->driver->set_sysclk)
  2290. return codec->driver->set_sysclk(codec, clk_id, source,
  2291. freq, dir);
  2292. else
  2293. return -EINVAL;
  2294. }
  2295. EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
  2296. /**
  2297. * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
  2298. * @dai: DAI
  2299. * @div_id: DAI specific clock divider ID
  2300. * @div: new clock divisor.
  2301. *
  2302. * Configures the clock dividers. This is used to derive the best DAI bit and
  2303. * frame clocks from the system or master clock. It's best to set the DAI bit
  2304. * and frame clocks as low as possible to save system power.
  2305. */
  2306. int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
  2307. int div_id, int div)
  2308. {
  2309. if (dai->driver && dai->driver->ops->set_clkdiv)
  2310. return dai->driver->ops->set_clkdiv(dai, div_id, div);
  2311. else
  2312. return -EINVAL;
  2313. }
  2314. EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
  2315. /**
  2316. * snd_soc_dai_set_pll - configure DAI PLL.
  2317. * @dai: DAI
  2318. * @pll_id: DAI specific PLL ID
  2319. * @source: DAI specific source for the PLL
  2320. * @freq_in: PLL input clock frequency in Hz
  2321. * @freq_out: requested PLL output clock frequency in Hz
  2322. *
  2323. * Configures and enables PLL to generate output clock based on input clock.
  2324. */
  2325. int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
  2326. unsigned int freq_in, unsigned int freq_out)
  2327. {
  2328. if (dai->driver && dai->driver->ops->set_pll)
  2329. return dai->driver->ops->set_pll(dai, pll_id, source,
  2330. freq_in, freq_out);
  2331. else if (dai->codec && dai->codec->driver->set_pll)
  2332. return dai->codec->driver->set_pll(dai->codec, pll_id, source,
  2333. freq_in, freq_out);
  2334. else
  2335. return -EINVAL;
  2336. }
  2337. EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
  2338. /*
  2339. * snd_soc_codec_set_pll - configure codec PLL.
  2340. * @codec: CODEC
  2341. * @pll_id: DAI specific PLL ID
  2342. * @source: DAI specific source for the PLL
  2343. * @freq_in: PLL input clock frequency in Hz
  2344. * @freq_out: requested PLL output clock frequency in Hz
  2345. *
  2346. * Configures and enables PLL to generate output clock based on input clock.
  2347. */
  2348. int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
  2349. unsigned int freq_in, unsigned int freq_out)
  2350. {
  2351. if (codec->driver->set_pll)
  2352. return codec->driver->set_pll(codec, pll_id, source,
  2353. freq_in, freq_out);
  2354. else
  2355. return -EINVAL;
  2356. }
  2357. EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
  2358. /**
  2359. * snd_soc_dai_set_fmt - configure DAI hardware audio format.
  2360. * @dai: DAI
  2361. * @fmt: SND_SOC_DAIFMT_ format value.
  2362. *
  2363. * Configures the DAI hardware format and clocking.
  2364. */
  2365. int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  2366. {
  2367. if (dai->driver && dai->driver->ops->set_fmt)
  2368. return dai->driver->ops->set_fmt(dai, fmt);
  2369. else
  2370. return -EINVAL;
  2371. }
  2372. EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
  2373. /**
  2374. * snd_soc_dai_set_tdm_slot - configure DAI TDM.
  2375. * @dai: DAI
  2376. * @tx_mask: bitmask representing active TX slots.
  2377. * @rx_mask: bitmask representing active RX slots.
  2378. * @slots: Number of slots in use.
  2379. * @slot_width: Width in bits for each slot.
  2380. *
  2381. * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
  2382. * specific.
  2383. */
  2384. int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
  2385. unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
  2386. {
  2387. if (dai->driver && dai->driver->ops->set_tdm_slot)
  2388. return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
  2389. slots, slot_width);
  2390. else
  2391. return -EINVAL;
  2392. }
  2393. EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
  2394. /**
  2395. * snd_soc_dai_set_channel_map - configure DAI audio channel map
  2396. * @dai: DAI
  2397. * @tx_num: how many TX channels
  2398. * @tx_slot: pointer to an array which imply the TX slot number channel
  2399. * 0~num-1 uses
  2400. * @rx_num: how many RX channels
  2401. * @rx_slot: pointer to an array which imply the RX slot number channel
  2402. * 0~num-1 uses
  2403. *
  2404. * configure the relationship between channel number and TDM slot number.
  2405. */
  2406. int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
  2407. unsigned int tx_num, unsigned int *tx_slot,
  2408. unsigned int rx_num, unsigned int *rx_slot)
  2409. {
  2410. if (dai->driver && dai->driver->ops->set_channel_map)
  2411. return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
  2412. rx_num, rx_slot);
  2413. else
  2414. return -EINVAL;
  2415. }
  2416. EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
  2417. /**
  2418. * snd_soc_dai_set_tristate - configure DAI system or master clock.
  2419. * @dai: DAI
  2420. * @tristate: tristate enable
  2421. *
  2422. * Tristates the DAI so that others can use it.
  2423. */
  2424. int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
  2425. {
  2426. if (dai->driver && dai->driver->ops->set_tristate)
  2427. return dai->driver->ops->set_tristate(dai, tristate);
  2428. else
  2429. return -EINVAL;
  2430. }
  2431. EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
  2432. /**
  2433. * snd_soc_dai_digital_mute - configure DAI system or master clock.
  2434. * @dai: DAI
  2435. * @mute: mute enable
  2436. *
  2437. * Mutes the DAI DAC.
  2438. */
  2439. int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
  2440. {
  2441. if (dai->driver && dai->driver->ops->digital_mute)
  2442. return dai->driver->ops->digital_mute(dai, mute);
  2443. else
  2444. return -EINVAL;
  2445. }
  2446. EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
  2447. /**
  2448. * snd_soc_register_card - Register a card with the ASoC core
  2449. *
  2450. * @card: Card to register
  2451. *
  2452. */
  2453. int snd_soc_register_card(struct snd_soc_card *card)
  2454. {
  2455. int i;
  2456. if (!card->name || !card->dev)
  2457. return -EINVAL;
  2458. for (i = 0; i < card->num_links; i++) {
  2459. struct snd_soc_dai_link *link = &card->dai_link[i];
  2460. /*
  2461. * Codec must be specified by 1 of name or OF node,
  2462. * not both or neither.
  2463. */
  2464. if (!!link->codec_name == !!link->codec_of_node) {
  2465. dev_err(card->dev,
  2466. "Neither/both codec name/of_node are set for %s\n",
  2467. link->name);
  2468. return -EINVAL;
  2469. }
  2470. /*
  2471. * Platform may be specified by either name or OF node, but
  2472. * can be left unspecified, and a dummy platform will be used.
  2473. */
  2474. if (link->platform_name && link->platform_of_node) {
  2475. dev_err(card->dev,
  2476. "Both platform name/of_node are set for %s\n", link->name);
  2477. return -EINVAL;
  2478. }
  2479. /*
  2480. * CPU DAI must be specified by 1 of name or OF node,
  2481. * not both or neither.
  2482. */
  2483. if (!!link->cpu_dai_name == !!link->cpu_dai_of_node) {
  2484. dev_err(card->dev,
  2485. "Neither/both cpu_dai name/of_node are set for %s\n",
  2486. link->name);
  2487. return -EINVAL;
  2488. }
  2489. }
  2490. dev_set_drvdata(card->dev, card);
  2491. snd_soc_initialize_card_lists(card);
  2492. soc_init_card_debugfs(card);
  2493. card->rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime) *
  2494. (card->num_links + card->num_aux_devs),
  2495. GFP_KERNEL);
  2496. if (card->rtd == NULL)
  2497. return -ENOMEM;
  2498. card->rtd_aux = &card->rtd[card->num_links];
  2499. for (i = 0; i < card->num_links; i++)
  2500. card->rtd[i].dai_link = &card->dai_link[i];
  2501. INIT_LIST_HEAD(&card->list);
  2502. INIT_LIST_HEAD(&card->dapm_dirty);
  2503. card->instantiated = 0;
  2504. mutex_init(&card->mutex);
  2505. mutex_lock(&client_mutex);
  2506. list_add(&card->list, &card_list);
  2507. snd_soc_instantiate_cards();
  2508. mutex_unlock(&client_mutex);
  2509. dev_dbg(card->dev, "Registered card '%s'\n", card->name);
  2510. return 0;
  2511. }
  2512. EXPORT_SYMBOL_GPL(snd_soc_register_card);
  2513. /**
  2514. * snd_soc_unregister_card - Unregister a card with the ASoC core
  2515. *
  2516. * @card: Card to unregister
  2517. *
  2518. */
  2519. int snd_soc_unregister_card(struct snd_soc_card *card)
  2520. {
  2521. if (card->instantiated)
  2522. soc_cleanup_card_resources(card);
  2523. mutex_lock(&client_mutex);
  2524. list_del(&card->list);
  2525. mutex_unlock(&client_mutex);
  2526. dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
  2527. return 0;
  2528. }
  2529. EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
  2530. /*
  2531. * Simplify DAI link configuration by removing ".-1" from device names
  2532. * and sanitizing names.
  2533. */
  2534. static char *fmt_single_name(struct device *dev, int *id)
  2535. {
  2536. char *found, name[NAME_SIZE];
  2537. int id1, id2;
  2538. if (dev_name(dev) == NULL)
  2539. return NULL;
  2540. strlcpy(name, dev_name(dev), NAME_SIZE);
  2541. /* are we a "%s.%d" name (platform and SPI components) */
  2542. found = strstr(name, dev->driver->name);
  2543. if (found) {
  2544. /* get ID */
  2545. if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
  2546. /* discard ID from name if ID == -1 */
  2547. if (*id == -1)
  2548. found[strlen(dev->driver->name)] = '\0';
  2549. }
  2550. } else {
  2551. /* I2C component devices are named "bus-addr" */
  2552. if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
  2553. char tmp[NAME_SIZE];
  2554. /* create unique ID number from I2C addr and bus */
  2555. *id = ((id1 & 0xffff) << 16) + id2;
  2556. /* sanitize component name for DAI link creation */
  2557. snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
  2558. strlcpy(name, tmp, NAME_SIZE);
  2559. } else
  2560. *id = 0;
  2561. }
  2562. return kstrdup(name, GFP_KERNEL);
  2563. }
  2564. /*
  2565. * Simplify DAI link naming for single devices with multiple DAIs by removing
  2566. * any ".-1" and using the DAI name (instead of device name).
  2567. */
  2568. static inline char *fmt_multiple_name(struct device *dev,
  2569. struct snd_soc_dai_driver *dai_drv)
  2570. {
  2571. if (dai_drv->name == NULL) {
  2572. printk(KERN_ERR "asoc: error - multiple DAI %s registered with no name\n",
  2573. dev_name(dev));
  2574. return NULL;
  2575. }
  2576. return kstrdup(dai_drv->name, GFP_KERNEL);
  2577. }
  2578. /**
  2579. * snd_soc_register_dai - Register a DAI with the ASoC core
  2580. *
  2581. * @dai: DAI to register
  2582. */
  2583. int snd_soc_register_dai(struct device *dev,
  2584. struct snd_soc_dai_driver *dai_drv)
  2585. {
  2586. struct snd_soc_dai *dai;
  2587. dev_dbg(dev, "dai register %s\n", dev_name(dev));
  2588. dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
  2589. if (dai == NULL)
  2590. return -ENOMEM;
  2591. /* create DAI component name */
  2592. dai->name = fmt_single_name(dev, &dai->id);
  2593. if (dai->name == NULL) {
  2594. kfree(dai);
  2595. return -ENOMEM;
  2596. }
  2597. dai->dev = dev;
  2598. dai->driver = dai_drv;
  2599. if (!dai->driver->ops)
  2600. dai->driver->ops = &null_dai_ops;
  2601. mutex_lock(&client_mutex);
  2602. list_add(&dai->list, &dai_list);
  2603. snd_soc_instantiate_cards();
  2604. mutex_unlock(&client_mutex);
  2605. pr_debug("Registered DAI '%s'\n", dai->name);
  2606. return 0;
  2607. }
  2608. EXPORT_SYMBOL_GPL(snd_soc_register_dai);
  2609. /**
  2610. * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
  2611. *
  2612. * @dai: DAI to unregister
  2613. */
  2614. void snd_soc_unregister_dai(struct device *dev)
  2615. {
  2616. struct snd_soc_dai *dai;
  2617. list_for_each_entry(dai, &dai_list, list) {
  2618. if (dev == dai->dev)
  2619. goto found;
  2620. }
  2621. return;
  2622. found:
  2623. mutex_lock(&client_mutex);
  2624. list_del(&dai->list);
  2625. mutex_unlock(&client_mutex);
  2626. pr_debug("Unregistered DAI '%s'\n", dai->name);
  2627. kfree(dai->name);
  2628. kfree(dai);
  2629. }
  2630. EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
  2631. /**
  2632. * snd_soc_register_dais - Register multiple DAIs with the ASoC core
  2633. *
  2634. * @dai: Array of DAIs to register
  2635. * @count: Number of DAIs
  2636. */
  2637. int snd_soc_register_dais(struct device *dev,
  2638. struct snd_soc_dai_driver *dai_drv, size_t count)
  2639. {
  2640. struct snd_soc_dai *dai;
  2641. int i, ret = 0;
  2642. dev_dbg(dev, "dai register %s #%Zu\n", dev_name(dev), count);
  2643. for (i = 0; i < count; i++) {
  2644. dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
  2645. if (dai == NULL) {
  2646. ret = -ENOMEM;
  2647. goto err;
  2648. }
  2649. /* create DAI component name */
  2650. dai->name = fmt_multiple_name(dev, &dai_drv[i]);
  2651. if (dai->name == NULL) {
  2652. kfree(dai);
  2653. ret = -EINVAL;
  2654. goto err;
  2655. }
  2656. dai->dev = dev;
  2657. dai->driver = &dai_drv[i];
  2658. if (dai->driver->id)
  2659. dai->id = dai->driver->id;
  2660. else
  2661. dai->id = i;
  2662. if (!dai->driver->ops)
  2663. dai->driver->ops = &null_dai_ops;
  2664. mutex_lock(&client_mutex);
  2665. list_add(&dai->list, &dai_list);
  2666. mutex_unlock(&client_mutex);
  2667. pr_debug("Registered DAI '%s'\n", dai->name);
  2668. }
  2669. mutex_lock(&client_mutex);
  2670. snd_soc_instantiate_cards();
  2671. mutex_unlock(&client_mutex);
  2672. return 0;
  2673. err:
  2674. for (i--; i >= 0; i--)
  2675. snd_soc_unregister_dai(dev);
  2676. return ret;
  2677. }
  2678. EXPORT_SYMBOL_GPL(snd_soc_register_dais);
  2679. /**
  2680. * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
  2681. *
  2682. * @dai: Array of DAIs to unregister
  2683. * @count: Number of DAIs
  2684. */
  2685. void snd_soc_unregister_dais(struct device *dev, size_t count)
  2686. {
  2687. int i;
  2688. for (i = 0; i < count; i++)
  2689. snd_soc_unregister_dai(dev);
  2690. }
  2691. EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
  2692. /**
  2693. * snd_soc_register_platform - Register a platform with the ASoC core
  2694. *
  2695. * @platform: platform to register
  2696. */
  2697. int snd_soc_register_platform(struct device *dev,
  2698. struct snd_soc_platform_driver *platform_drv)
  2699. {
  2700. struct snd_soc_platform *platform;
  2701. dev_dbg(dev, "platform register %s\n", dev_name(dev));
  2702. platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
  2703. if (platform == NULL)
  2704. return -ENOMEM;
  2705. /* create platform component name */
  2706. platform->name = fmt_single_name(dev, &platform->id);
  2707. if (platform->name == NULL) {
  2708. kfree(platform);
  2709. return -ENOMEM;
  2710. }
  2711. platform->dev = dev;
  2712. platform->driver = platform_drv;
  2713. platform->dapm.dev = dev;
  2714. platform->dapm.platform = platform;
  2715. platform->dapm.stream_event = platform_drv->stream_event;
  2716. mutex_lock(&client_mutex);
  2717. list_add(&platform->list, &platform_list);
  2718. snd_soc_instantiate_cards();
  2719. mutex_unlock(&client_mutex);
  2720. pr_debug("Registered platform '%s'\n", platform->name);
  2721. return 0;
  2722. }
  2723. EXPORT_SYMBOL_GPL(snd_soc_register_platform);
  2724. /**
  2725. * snd_soc_unregister_platform - Unregister a platform from the ASoC core
  2726. *
  2727. * @platform: platform to unregister
  2728. */
  2729. void snd_soc_unregister_platform(struct device *dev)
  2730. {
  2731. struct snd_soc_platform *platform;
  2732. list_for_each_entry(platform, &platform_list, list) {
  2733. if (dev == platform->dev)
  2734. goto found;
  2735. }
  2736. return;
  2737. found:
  2738. mutex_lock(&client_mutex);
  2739. list_del(&platform->list);
  2740. mutex_unlock(&client_mutex);
  2741. pr_debug("Unregistered platform '%s'\n", platform->name);
  2742. kfree(platform->name);
  2743. kfree(platform);
  2744. }
  2745. EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
  2746. static u64 codec_format_map[] = {
  2747. SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
  2748. SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
  2749. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
  2750. SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
  2751. SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
  2752. SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
  2753. SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
  2754. SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
  2755. SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
  2756. SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
  2757. SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
  2758. SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
  2759. SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
  2760. SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
  2761. SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
  2762. | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
  2763. };
  2764. /* Fix up the DAI formats for endianness: codecs don't actually see
  2765. * the endianness of the data but we're using the CPU format
  2766. * definitions which do need to include endianness so we ensure that
  2767. * codec DAIs always have both big and little endian variants set.
  2768. */
  2769. static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
  2770. {
  2771. int i;
  2772. for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
  2773. if (stream->formats & codec_format_map[i])
  2774. stream->formats |= codec_format_map[i];
  2775. }
  2776. /**
  2777. * snd_soc_register_codec - Register a codec with the ASoC core
  2778. *
  2779. * @codec: codec to register
  2780. */
  2781. int snd_soc_register_codec(struct device *dev,
  2782. const struct snd_soc_codec_driver *codec_drv,
  2783. struct snd_soc_dai_driver *dai_drv,
  2784. int num_dai)
  2785. {
  2786. size_t reg_size;
  2787. struct snd_soc_codec *codec;
  2788. int ret, i;
  2789. dev_dbg(dev, "codec register %s\n", dev_name(dev));
  2790. codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
  2791. if (codec == NULL)
  2792. return -ENOMEM;
  2793. /* create CODEC component name */
  2794. codec->name = fmt_single_name(dev, &codec->id);
  2795. if (codec->name == NULL) {
  2796. kfree(codec);
  2797. return -ENOMEM;
  2798. }
  2799. if (codec_drv->compress_type)
  2800. codec->compress_type = codec_drv->compress_type;
  2801. else
  2802. codec->compress_type = SND_SOC_FLAT_COMPRESSION;
  2803. codec->write = codec_drv->write;
  2804. codec->read = codec_drv->read;
  2805. codec->volatile_register = codec_drv->volatile_register;
  2806. codec->readable_register = codec_drv->readable_register;
  2807. codec->writable_register = codec_drv->writable_register;
  2808. codec->dapm.bias_level = SND_SOC_BIAS_OFF;
  2809. codec->dapm.dev = dev;
  2810. codec->dapm.codec = codec;
  2811. codec->dapm.seq_notifier = codec_drv->seq_notifier;
  2812. codec->dapm.stream_event = codec_drv->stream_event;
  2813. codec->dev = dev;
  2814. codec->driver = codec_drv;
  2815. codec->num_dai = num_dai;
  2816. mutex_init(&codec->mutex);
  2817. /* allocate CODEC register cache */
  2818. if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
  2819. reg_size = codec_drv->reg_cache_size * codec_drv->reg_word_size;
  2820. codec->reg_size = reg_size;
  2821. /* it is necessary to make a copy of the default register cache
  2822. * because in the case of using a compression type that requires
  2823. * the default register cache to be marked as __devinitconst the
  2824. * kernel might have freed the array by the time we initialize
  2825. * the cache.
  2826. */
  2827. if (codec_drv->reg_cache_default) {
  2828. codec->reg_def_copy = kmemdup(codec_drv->reg_cache_default,
  2829. reg_size, GFP_KERNEL);
  2830. if (!codec->reg_def_copy) {
  2831. ret = -ENOMEM;
  2832. goto fail;
  2833. }
  2834. }
  2835. }
  2836. if (codec_drv->reg_access_size && codec_drv->reg_access_default) {
  2837. if (!codec->volatile_register)
  2838. codec->volatile_register = snd_soc_default_volatile_register;
  2839. if (!codec->readable_register)
  2840. codec->readable_register = snd_soc_default_readable_register;
  2841. if (!codec->writable_register)
  2842. codec->writable_register = snd_soc_default_writable_register;
  2843. }
  2844. for (i = 0; i < num_dai; i++) {
  2845. fixup_codec_formats(&dai_drv[i].playback);
  2846. fixup_codec_formats(&dai_drv[i].capture);
  2847. }
  2848. /* register any DAIs */
  2849. if (num_dai) {
  2850. ret = snd_soc_register_dais(dev, dai_drv, num_dai);
  2851. if (ret < 0)
  2852. goto fail;
  2853. }
  2854. mutex_lock(&client_mutex);
  2855. list_add(&codec->list, &codec_list);
  2856. snd_soc_instantiate_cards();
  2857. mutex_unlock(&client_mutex);
  2858. pr_debug("Registered codec '%s'\n", codec->name);
  2859. return 0;
  2860. fail:
  2861. kfree(codec->reg_def_copy);
  2862. codec->reg_def_copy = NULL;
  2863. kfree(codec->name);
  2864. kfree(codec);
  2865. return ret;
  2866. }
  2867. EXPORT_SYMBOL_GPL(snd_soc_register_codec);
  2868. /**
  2869. * snd_soc_unregister_codec - Unregister a codec from the ASoC core
  2870. *
  2871. * @codec: codec to unregister
  2872. */
  2873. void snd_soc_unregister_codec(struct device *dev)
  2874. {
  2875. struct snd_soc_codec *codec;
  2876. int i;
  2877. list_for_each_entry(codec, &codec_list, list) {
  2878. if (dev == codec->dev)
  2879. goto found;
  2880. }
  2881. return;
  2882. found:
  2883. if (codec->num_dai)
  2884. for (i = 0; i < codec->num_dai; i++)
  2885. snd_soc_unregister_dai(dev);
  2886. mutex_lock(&client_mutex);
  2887. list_del(&codec->list);
  2888. mutex_unlock(&client_mutex);
  2889. pr_debug("Unregistered codec '%s'\n", codec->name);
  2890. snd_soc_cache_exit(codec);
  2891. kfree(codec->reg_def_copy);
  2892. kfree(codec->name);
  2893. kfree(codec);
  2894. }
  2895. EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
  2896. /* Retrieve a card's name from device tree */
  2897. int snd_soc_of_parse_card_name(struct snd_soc_card *card,
  2898. const char *propname)
  2899. {
  2900. struct device_node *np = card->dev->of_node;
  2901. int ret;
  2902. ret = of_property_read_string_index(np, propname, 0, &card->name);
  2903. /*
  2904. * EINVAL means the property does not exist. This is fine providing
  2905. * card->name was previously set, which is checked later in
  2906. * snd_soc_register_card.
  2907. */
  2908. if (ret < 0 && ret != -EINVAL) {
  2909. dev_err(card->dev,
  2910. "Property '%s' could not be read: %d\n",
  2911. propname, ret);
  2912. return ret;
  2913. }
  2914. return 0;
  2915. }
  2916. EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
  2917. int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
  2918. const char *propname)
  2919. {
  2920. struct device_node *np = card->dev->of_node;
  2921. int num_routes;
  2922. struct snd_soc_dapm_route *routes;
  2923. int i, ret;
  2924. num_routes = of_property_count_strings(np, propname);
  2925. if (num_routes & 1) {
  2926. dev_err(card->dev,
  2927. "Property '%s's length is not even\n",
  2928. propname);
  2929. return -EINVAL;
  2930. }
  2931. num_routes /= 2;
  2932. if (!num_routes) {
  2933. dev_err(card->dev,
  2934. "Property '%s's length is zero\n",
  2935. propname);
  2936. return -EINVAL;
  2937. }
  2938. routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
  2939. GFP_KERNEL);
  2940. if (!routes) {
  2941. dev_err(card->dev,
  2942. "Could not allocate DAPM route table\n");
  2943. return -EINVAL;
  2944. }
  2945. for (i = 0; i < num_routes; i++) {
  2946. ret = of_property_read_string_index(np, propname,
  2947. 2 * i, &routes[i].sink);
  2948. if (ret) {
  2949. dev_err(card->dev,
  2950. "Property '%s' index %d could not be read: %d\n",
  2951. propname, 2 * i, ret);
  2952. return -EINVAL;
  2953. }
  2954. ret = of_property_read_string_index(np, propname,
  2955. (2 * i) + 1, &routes[i].source);
  2956. if (ret) {
  2957. dev_err(card->dev,
  2958. "Property '%s' index %d could not be read: %d\n",
  2959. propname, (2 * i) + 1, ret);
  2960. return -EINVAL;
  2961. }
  2962. }
  2963. card->num_dapm_routes = num_routes;
  2964. card->dapm_routes = routes;
  2965. return 0;
  2966. }
  2967. EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
  2968. static int __init snd_soc_init(void)
  2969. {
  2970. #ifdef CONFIG_DEBUG_FS
  2971. snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
  2972. if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
  2973. printk(KERN_WARNING
  2974. "ASoC: Failed to create debugfs directory\n");
  2975. snd_soc_debugfs_root = NULL;
  2976. }
  2977. if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
  2978. &codec_list_fops))
  2979. pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
  2980. if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
  2981. &dai_list_fops))
  2982. pr_warn("ASoC: Failed to create DAI list debugfs file\n");
  2983. if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
  2984. &platform_list_fops))
  2985. pr_warn("ASoC: Failed to create platform list debugfs file\n");
  2986. #endif
  2987. snd_soc_util_init();
  2988. return platform_driver_register(&soc_driver);
  2989. }
  2990. module_init(snd_soc_init);
  2991. static void __exit snd_soc_exit(void)
  2992. {
  2993. snd_soc_util_exit();
  2994. #ifdef CONFIG_DEBUG_FS
  2995. debugfs_remove_recursive(snd_soc_debugfs_root);
  2996. #endif
  2997. platform_driver_unregister(&soc_driver);
  2998. }
  2999. module_exit(snd_soc_exit);
  3000. /* Module information */
  3001. MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
  3002. MODULE_DESCRIPTION("ALSA SoC Core");
  3003. MODULE_LICENSE("GPL");
  3004. MODULE_ALIAS("platform:soc-audio");