soc-core.c 100 KB

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