soc-core.c 85 KB

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