soc-core.c 95 KB

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