soc-core.c 85 KB

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