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