soc-core.c 94 KB

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