soc-core.c 86 KB

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