soc-core.c 85 KB

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