soc-core.c 87 KB

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