soc-core.c 113 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477
  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/ctype.h>
  33. #include <linux/slab.h>
  34. #include <linux/of.h>
  35. #include <sound/ac97_codec.h>
  36. #include <sound/core.h>
  37. #include <sound/jack.h>
  38. #include <sound/pcm.h>
  39. #include <sound/pcm_params.h>
  40. #include <sound/soc.h>
  41. #include <sound/soc-dpcm.h>
  42. #include <sound/initval.h>
  43. #define CREATE_TRACE_POINTS
  44. #include <trace/events/asoc.h>
  45. #define NAME_SIZE 32
  46. #ifdef CONFIG_DEBUG_FS
  47. struct dentry *snd_soc_debugfs_root;
  48. EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
  49. #endif
  50. static DEFINE_MUTEX(client_mutex);
  51. static LIST_HEAD(dai_list);
  52. static LIST_HEAD(platform_list);
  53. static LIST_HEAD(codec_list);
  54. static LIST_HEAD(component_list);
  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 (!snd_soc_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 = dev_get_drvdata(dev);
  149. return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
  150. }
  151. static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
  152. static ssize_t pmdown_time_show(struct device *dev,
  153. struct device_attribute *attr, char *buf)
  154. {
  155. struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
  156. return sprintf(buf, "%ld\n", rtd->pmdown_time);
  157. }
  158. static ssize_t pmdown_time_set(struct device *dev,
  159. struct device_attribute *attr,
  160. const char *buf, size_t count)
  161. {
  162. struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
  163. int ret;
  164. ret = strict_strtol(buf, 10, &rtd->pmdown_time);
  165. if (ret)
  166. return ret;
  167. return count;
  168. }
  169. static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
  170. #ifdef CONFIG_DEBUG_FS
  171. static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
  172. size_t count, loff_t *ppos)
  173. {
  174. ssize_t ret;
  175. struct snd_soc_codec *codec = file->private_data;
  176. char *buf;
  177. if (*ppos < 0 || !count)
  178. return -EINVAL;
  179. buf = kmalloc(count, GFP_KERNEL);
  180. if (!buf)
  181. return -ENOMEM;
  182. ret = soc_codec_reg_show(codec, buf, count, *ppos);
  183. if (ret >= 0) {
  184. if (copy_to_user(user_buf, buf, ret)) {
  185. kfree(buf);
  186. return -EFAULT;
  187. }
  188. *ppos += ret;
  189. }
  190. kfree(buf);
  191. return ret;
  192. }
  193. static ssize_t codec_reg_write_file(struct file *file,
  194. const char __user *user_buf, size_t count, loff_t *ppos)
  195. {
  196. char buf[32];
  197. size_t buf_size;
  198. char *start = buf;
  199. unsigned long reg, value;
  200. struct snd_soc_codec *codec = file->private_data;
  201. buf_size = min(count, (sizeof(buf)-1));
  202. if (copy_from_user(buf, user_buf, buf_size))
  203. return -EFAULT;
  204. buf[buf_size] = 0;
  205. while (*start == ' ')
  206. start++;
  207. reg = simple_strtoul(start, &start, 16);
  208. while (*start == ' ')
  209. start++;
  210. if (strict_strtoul(start, 16, &value))
  211. return -EINVAL;
  212. /* Userspace has been fiddling around behind the kernel's back */
  213. add_taint(TAINT_USER, LOCKDEP_NOW_UNRELIABLE);
  214. snd_soc_write(codec, reg, value);
  215. return buf_size;
  216. }
  217. static const struct file_operations codec_reg_fops = {
  218. .open = simple_open,
  219. .read = codec_reg_read_file,
  220. .write = codec_reg_write_file,
  221. .llseek = default_llseek,
  222. };
  223. static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
  224. {
  225. struct dentry *debugfs_card_root = codec->card->debugfs_card_root;
  226. codec->debugfs_codec_root = debugfs_create_dir(codec->name,
  227. debugfs_card_root);
  228. if (!codec->debugfs_codec_root) {
  229. dev_warn(codec->dev,
  230. "ASoC: Failed to create codec debugfs directory\n");
  231. return;
  232. }
  233. debugfs_create_bool("cache_sync", 0444, codec->debugfs_codec_root,
  234. &codec->cache_sync);
  235. debugfs_create_bool("cache_only", 0444, codec->debugfs_codec_root,
  236. &codec->cache_only);
  237. codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
  238. codec->debugfs_codec_root,
  239. codec, &codec_reg_fops);
  240. if (!codec->debugfs_reg)
  241. dev_warn(codec->dev,
  242. "ASoC: Failed to create codec register debugfs file\n");
  243. snd_soc_dapm_debugfs_init(&codec->dapm, codec->debugfs_codec_root);
  244. }
  245. static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
  246. {
  247. debugfs_remove_recursive(codec->debugfs_codec_root);
  248. }
  249. static void soc_init_platform_debugfs(struct snd_soc_platform *platform)
  250. {
  251. struct dentry *debugfs_card_root = platform->card->debugfs_card_root;
  252. platform->debugfs_platform_root = debugfs_create_dir(platform->name,
  253. debugfs_card_root);
  254. if (!platform->debugfs_platform_root) {
  255. dev_warn(platform->dev,
  256. "ASoC: Failed to create platform debugfs directory\n");
  257. return;
  258. }
  259. snd_soc_dapm_debugfs_init(&platform->dapm,
  260. platform->debugfs_platform_root);
  261. }
  262. static void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
  263. {
  264. debugfs_remove_recursive(platform->debugfs_platform_root);
  265. }
  266. static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
  267. size_t count, loff_t *ppos)
  268. {
  269. char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  270. ssize_t len, ret = 0;
  271. struct snd_soc_codec *codec;
  272. if (!buf)
  273. return -ENOMEM;
  274. list_for_each_entry(codec, &codec_list, list) {
  275. len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
  276. codec->name);
  277. if (len >= 0)
  278. ret += len;
  279. if (ret > PAGE_SIZE) {
  280. ret = PAGE_SIZE;
  281. break;
  282. }
  283. }
  284. if (ret >= 0)
  285. ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
  286. kfree(buf);
  287. return ret;
  288. }
  289. static const struct file_operations codec_list_fops = {
  290. .read = codec_list_read_file,
  291. .llseek = default_llseek,/* read accesses f_pos */
  292. };
  293. static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
  294. size_t count, loff_t *ppos)
  295. {
  296. char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  297. ssize_t len, ret = 0;
  298. struct snd_soc_dai *dai;
  299. if (!buf)
  300. return -ENOMEM;
  301. list_for_each_entry(dai, &dai_list, list) {
  302. len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", dai->name);
  303. if (len >= 0)
  304. ret += len;
  305. if (ret > PAGE_SIZE) {
  306. ret = PAGE_SIZE;
  307. break;
  308. }
  309. }
  310. ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
  311. kfree(buf);
  312. return ret;
  313. }
  314. static const struct file_operations dai_list_fops = {
  315. .read = dai_list_read_file,
  316. .llseek = default_llseek,/* read accesses f_pos */
  317. };
  318. static ssize_t platform_list_read_file(struct file *file,
  319. char __user *user_buf,
  320. size_t count, loff_t *ppos)
  321. {
  322. char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  323. ssize_t len, ret = 0;
  324. struct snd_soc_platform *platform;
  325. if (!buf)
  326. return -ENOMEM;
  327. list_for_each_entry(platform, &platform_list, list) {
  328. len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
  329. platform->name);
  330. if (len >= 0)
  331. ret += len;
  332. if (ret > PAGE_SIZE) {
  333. ret = PAGE_SIZE;
  334. break;
  335. }
  336. }
  337. ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
  338. kfree(buf);
  339. return ret;
  340. }
  341. static const struct file_operations platform_list_fops = {
  342. .read = platform_list_read_file,
  343. .llseek = default_llseek,/* read accesses f_pos */
  344. };
  345. static void soc_init_card_debugfs(struct snd_soc_card *card)
  346. {
  347. card->debugfs_card_root = debugfs_create_dir(card->name,
  348. snd_soc_debugfs_root);
  349. if (!card->debugfs_card_root) {
  350. dev_warn(card->dev,
  351. "ASoC: Failed to create card debugfs directory\n");
  352. return;
  353. }
  354. card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
  355. card->debugfs_card_root,
  356. &card->pop_time);
  357. if (!card->debugfs_pop_time)
  358. dev_warn(card->dev,
  359. "ASoC: Failed to create pop time debugfs file\n");
  360. }
  361. static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
  362. {
  363. debugfs_remove_recursive(card->debugfs_card_root);
  364. }
  365. #else
  366. static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
  367. {
  368. }
  369. static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
  370. {
  371. }
  372. static inline void soc_init_platform_debugfs(struct snd_soc_platform *platform)
  373. {
  374. }
  375. static inline void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
  376. {
  377. }
  378. static inline void soc_init_card_debugfs(struct snd_soc_card *card)
  379. {
  380. }
  381. static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
  382. {
  383. }
  384. #endif
  385. struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
  386. const char *dai_link, int stream)
  387. {
  388. int i;
  389. for (i = 0; i < card->num_links; i++) {
  390. if (card->rtd[i].dai_link->no_pcm &&
  391. !strcmp(card->rtd[i].dai_link->name, dai_link))
  392. return card->rtd[i].pcm->streams[stream].substream;
  393. }
  394. dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
  395. return NULL;
  396. }
  397. EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
  398. struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
  399. const char *dai_link)
  400. {
  401. int i;
  402. for (i = 0; i < card->num_links; i++) {
  403. if (!strcmp(card->rtd[i].dai_link->name, dai_link))
  404. return &card->rtd[i];
  405. }
  406. dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
  407. return NULL;
  408. }
  409. EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
  410. #ifdef CONFIG_SND_SOC_AC97_BUS
  411. /* unregister ac97 codec */
  412. static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
  413. {
  414. if (codec->ac97->dev.bus)
  415. device_unregister(&codec->ac97->dev);
  416. return 0;
  417. }
  418. /* stop no dev release warning */
  419. static void soc_ac97_device_release(struct device *dev){}
  420. /* register ac97 codec to bus */
  421. static int soc_ac97_dev_register(struct snd_soc_codec *codec)
  422. {
  423. int err;
  424. codec->ac97->dev.bus = &ac97_bus_type;
  425. codec->ac97->dev.parent = codec->card->dev;
  426. codec->ac97->dev.release = soc_ac97_device_release;
  427. dev_set_name(&codec->ac97->dev, "%d-%d:%s",
  428. codec->card->snd_card->number, 0, codec->name);
  429. err = device_register(&codec->ac97->dev);
  430. if (err < 0) {
  431. dev_err(codec->dev, "ASoC: Can't register ac97 bus\n");
  432. codec->ac97->dev.bus = NULL;
  433. return err;
  434. }
  435. return 0;
  436. }
  437. #endif
  438. static void codec2codec_close_delayed_work(struct work_struct *work)
  439. {
  440. /* Currently nothing to do for c2c links
  441. * Since c2c links are internal nodes in the DAPM graph and
  442. * don't interface with the outside world or application layer
  443. * we don't have to do any special handling on close.
  444. */
  445. }
  446. #ifdef CONFIG_PM_SLEEP
  447. /* powers down audio subsystem for suspend */
  448. int snd_soc_suspend(struct device *dev)
  449. {
  450. struct snd_soc_card *card = dev_get_drvdata(dev);
  451. struct snd_soc_codec *codec;
  452. int i;
  453. /* If the initialization of this soc device failed, there is no codec
  454. * associated with it. Just bail out in this case.
  455. */
  456. if (list_empty(&card->codec_dev_list))
  457. return 0;
  458. /* Due to the resume being scheduled into a workqueue we could
  459. * suspend before that's finished - wait for it to complete.
  460. */
  461. snd_power_lock(card->snd_card);
  462. snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
  463. snd_power_unlock(card->snd_card);
  464. /* we're going to block userspace touching us until resume completes */
  465. snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
  466. /* mute any active DACs */
  467. for (i = 0; i < card->num_rtd; i++) {
  468. struct snd_soc_dai *dai = card->rtd[i].codec_dai;
  469. struct snd_soc_dai_driver *drv = dai->driver;
  470. if (card->rtd[i].dai_link->ignore_suspend)
  471. continue;
  472. if (drv->ops->digital_mute && dai->playback_active)
  473. drv->ops->digital_mute(dai, 1);
  474. }
  475. /* suspend all pcms */
  476. for (i = 0; i < card->num_rtd; i++) {
  477. if (card->rtd[i].dai_link->ignore_suspend)
  478. continue;
  479. snd_pcm_suspend_all(card->rtd[i].pcm);
  480. }
  481. if (card->suspend_pre)
  482. card->suspend_pre(card);
  483. for (i = 0; i < card->num_rtd; i++) {
  484. struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
  485. struct snd_soc_platform *platform = card->rtd[i].platform;
  486. if (card->rtd[i].dai_link->ignore_suspend)
  487. continue;
  488. if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
  489. cpu_dai->driver->suspend(cpu_dai);
  490. if (platform->driver->suspend && !platform->suspended) {
  491. platform->driver->suspend(cpu_dai);
  492. platform->suspended = 1;
  493. }
  494. }
  495. /* close any waiting streams and save state */
  496. for (i = 0; i < card->num_rtd; i++) {
  497. flush_delayed_work(&card->rtd[i].delayed_work);
  498. card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level;
  499. }
  500. for (i = 0; i < card->num_rtd; i++) {
  501. if (card->rtd[i].dai_link->ignore_suspend)
  502. continue;
  503. snd_soc_dapm_stream_event(&card->rtd[i],
  504. SNDRV_PCM_STREAM_PLAYBACK,
  505. SND_SOC_DAPM_STREAM_SUSPEND);
  506. snd_soc_dapm_stream_event(&card->rtd[i],
  507. SNDRV_PCM_STREAM_CAPTURE,
  508. SND_SOC_DAPM_STREAM_SUSPEND);
  509. }
  510. /* Recheck all analogue paths too */
  511. dapm_mark_io_dirty(&card->dapm);
  512. snd_soc_dapm_sync(&card->dapm);
  513. /* suspend all CODECs */
  514. list_for_each_entry(codec, &card->codec_dev_list, card_list) {
  515. /* If there are paths active then the CODEC will be held with
  516. * bias _ON and should not be suspended. */
  517. if (!codec->suspended && codec->driver->suspend) {
  518. switch (codec->dapm.bias_level) {
  519. case SND_SOC_BIAS_STANDBY:
  520. /*
  521. * If the CODEC is capable of idle
  522. * bias off then being in STANDBY
  523. * means it's doing something,
  524. * otherwise fall through.
  525. */
  526. if (codec->dapm.idle_bias_off) {
  527. dev_dbg(codec->dev,
  528. "ASoC: idle_bias_off CODEC on over suspend\n");
  529. break;
  530. }
  531. case SND_SOC_BIAS_OFF:
  532. codec->driver->suspend(codec);
  533. codec->suspended = 1;
  534. codec->cache_sync = 1;
  535. if (codec->using_regmap)
  536. regcache_mark_dirty(codec->control_data);
  537. break;
  538. default:
  539. dev_dbg(codec->dev,
  540. "ASoC: CODEC is on over suspend\n");
  541. break;
  542. }
  543. }
  544. }
  545. for (i = 0; i < card->num_rtd; i++) {
  546. struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
  547. if (card->rtd[i].dai_link->ignore_suspend)
  548. continue;
  549. if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
  550. cpu_dai->driver->suspend(cpu_dai);
  551. }
  552. if (card->suspend_post)
  553. card->suspend_post(card);
  554. return 0;
  555. }
  556. EXPORT_SYMBOL_GPL(snd_soc_suspend);
  557. /* deferred resume work, so resume can complete before we finished
  558. * setting our codec back up, which can be very slow on I2C
  559. */
  560. static void soc_resume_deferred(struct work_struct *work)
  561. {
  562. struct snd_soc_card *card =
  563. container_of(work, struct snd_soc_card, deferred_resume_work);
  564. struct snd_soc_codec *codec;
  565. int i;
  566. /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
  567. * so userspace apps are blocked from touching us
  568. */
  569. dev_dbg(card->dev, "ASoC: starting resume work\n");
  570. /* Bring us up into D2 so that DAPM starts enabling things */
  571. snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
  572. if (card->resume_pre)
  573. card->resume_pre(card);
  574. /* resume AC97 DAIs */
  575. for (i = 0; i < card->num_rtd; i++) {
  576. struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
  577. if (card->rtd[i].dai_link->ignore_suspend)
  578. continue;
  579. if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
  580. cpu_dai->driver->resume(cpu_dai);
  581. }
  582. list_for_each_entry(codec, &card->codec_dev_list, card_list) {
  583. /* If the CODEC was idle over suspend then it will have been
  584. * left with bias OFF or STANDBY and suspended so we must now
  585. * resume. Otherwise the suspend was suppressed.
  586. */
  587. if (codec->driver->resume && codec->suspended) {
  588. switch (codec->dapm.bias_level) {
  589. case SND_SOC_BIAS_STANDBY:
  590. case SND_SOC_BIAS_OFF:
  591. codec->driver->resume(codec);
  592. codec->suspended = 0;
  593. break;
  594. default:
  595. dev_dbg(codec->dev,
  596. "ASoC: CODEC was on over suspend\n");
  597. break;
  598. }
  599. }
  600. }
  601. for (i = 0; i < card->num_rtd; i++) {
  602. if (card->rtd[i].dai_link->ignore_suspend)
  603. continue;
  604. snd_soc_dapm_stream_event(&card->rtd[i],
  605. SNDRV_PCM_STREAM_PLAYBACK,
  606. SND_SOC_DAPM_STREAM_RESUME);
  607. snd_soc_dapm_stream_event(&card->rtd[i],
  608. SNDRV_PCM_STREAM_CAPTURE,
  609. SND_SOC_DAPM_STREAM_RESUME);
  610. }
  611. /* unmute any active DACs */
  612. for (i = 0; i < card->num_rtd; i++) {
  613. struct snd_soc_dai *dai = card->rtd[i].codec_dai;
  614. struct snd_soc_dai_driver *drv = dai->driver;
  615. if (card->rtd[i].dai_link->ignore_suspend)
  616. continue;
  617. if (drv->ops->digital_mute && dai->playback_active)
  618. drv->ops->digital_mute(dai, 0);
  619. }
  620. for (i = 0; i < card->num_rtd; i++) {
  621. struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
  622. struct snd_soc_platform *platform = card->rtd[i].platform;
  623. if (card->rtd[i].dai_link->ignore_suspend)
  624. continue;
  625. if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
  626. cpu_dai->driver->resume(cpu_dai);
  627. if (platform->driver->resume && platform->suspended) {
  628. platform->driver->resume(cpu_dai);
  629. platform->suspended = 0;
  630. }
  631. }
  632. if (card->resume_post)
  633. card->resume_post(card);
  634. dev_dbg(card->dev, "ASoC: resume work completed\n");
  635. /* userspace can access us now we are back as we were before */
  636. snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
  637. /* Recheck all analogue paths too */
  638. dapm_mark_io_dirty(&card->dapm);
  639. snd_soc_dapm_sync(&card->dapm);
  640. }
  641. /* powers up audio subsystem after a suspend */
  642. int snd_soc_resume(struct device *dev)
  643. {
  644. struct snd_soc_card *card = dev_get_drvdata(dev);
  645. int i, ac97_control = 0;
  646. /* If the initialization of this soc device failed, there is no codec
  647. * associated with it. Just bail out in this case.
  648. */
  649. if (list_empty(&card->codec_dev_list))
  650. return 0;
  651. /* AC97 devices might have other drivers hanging off them so
  652. * need to resume immediately. Other drivers don't have that
  653. * problem and may take a substantial amount of time to resume
  654. * due to I/O costs and anti-pop so handle them out of line.
  655. */
  656. for (i = 0; i < card->num_rtd; i++) {
  657. struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
  658. ac97_control |= cpu_dai->driver->ac97_control;
  659. }
  660. if (ac97_control) {
  661. dev_dbg(dev, "ASoC: Resuming AC97 immediately\n");
  662. soc_resume_deferred(&card->deferred_resume_work);
  663. } else {
  664. dev_dbg(dev, "ASoC: Scheduling resume work\n");
  665. if (!schedule_work(&card->deferred_resume_work))
  666. dev_err(dev, "ASoC: resume work item may be lost\n");
  667. }
  668. return 0;
  669. }
  670. EXPORT_SYMBOL_GPL(snd_soc_resume);
  671. #else
  672. #define snd_soc_suspend NULL
  673. #define snd_soc_resume NULL
  674. #endif
  675. static const struct snd_soc_dai_ops null_dai_ops = {
  676. };
  677. static int soc_bind_dai_link(struct snd_soc_card *card, int num)
  678. {
  679. struct snd_soc_dai_link *dai_link = &card->dai_link[num];
  680. struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
  681. struct snd_soc_codec *codec;
  682. struct snd_soc_platform *platform;
  683. struct snd_soc_dai *codec_dai, *cpu_dai;
  684. const char *platform_name;
  685. dev_dbg(card->dev, "ASoC: binding %s at idx %d\n", dai_link->name, num);
  686. /* Find CPU DAI from registered DAIs*/
  687. list_for_each_entry(cpu_dai, &dai_list, list) {
  688. if (dai_link->cpu_of_node &&
  689. (cpu_dai->dev->of_node != dai_link->cpu_of_node))
  690. continue;
  691. if (dai_link->cpu_name &&
  692. strcmp(dev_name(cpu_dai->dev), dai_link->cpu_name))
  693. continue;
  694. if (dai_link->cpu_dai_name &&
  695. strcmp(cpu_dai->name, dai_link->cpu_dai_name))
  696. continue;
  697. rtd->cpu_dai = cpu_dai;
  698. }
  699. if (!rtd->cpu_dai) {
  700. dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
  701. dai_link->cpu_dai_name);
  702. return -EPROBE_DEFER;
  703. }
  704. /* Find CODEC from registered CODECs */
  705. list_for_each_entry(codec, &codec_list, list) {
  706. if (dai_link->codec_of_node) {
  707. if (codec->dev->of_node != dai_link->codec_of_node)
  708. continue;
  709. } else {
  710. if (strcmp(codec->name, dai_link->codec_name))
  711. continue;
  712. }
  713. rtd->codec = codec;
  714. /*
  715. * CODEC found, so find CODEC DAI from registered DAIs from
  716. * this CODEC
  717. */
  718. list_for_each_entry(codec_dai, &dai_list, list) {
  719. if (codec->dev == codec_dai->dev &&
  720. !strcmp(codec_dai->name,
  721. dai_link->codec_dai_name)) {
  722. rtd->codec_dai = codec_dai;
  723. }
  724. }
  725. if (!rtd->codec_dai) {
  726. dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
  727. dai_link->codec_dai_name);
  728. return -EPROBE_DEFER;
  729. }
  730. }
  731. if (!rtd->codec) {
  732. dev_err(card->dev, "ASoC: CODEC %s not registered\n",
  733. dai_link->codec_name);
  734. return -EPROBE_DEFER;
  735. }
  736. /* if there's no platform we match on the empty platform */
  737. platform_name = dai_link->platform_name;
  738. if (!platform_name && !dai_link->platform_of_node)
  739. platform_name = "snd-soc-dummy";
  740. /* find one from the set of registered platforms */
  741. list_for_each_entry(platform, &platform_list, list) {
  742. if (dai_link->platform_of_node) {
  743. if (platform->dev->of_node !=
  744. dai_link->platform_of_node)
  745. continue;
  746. } else {
  747. if (strcmp(platform->name, platform_name))
  748. continue;
  749. }
  750. rtd->platform = platform;
  751. }
  752. if (!rtd->platform) {
  753. dev_err(card->dev, "ASoC: platform %s not registered\n",
  754. dai_link->platform_name);
  755. return -EPROBE_DEFER;
  756. }
  757. card->num_rtd++;
  758. return 0;
  759. }
  760. static int soc_remove_platform(struct snd_soc_platform *platform)
  761. {
  762. int ret;
  763. if (platform->driver->remove) {
  764. ret = platform->driver->remove(platform);
  765. if (ret < 0)
  766. dev_err(platform->dev, "ASoC: failed to remove %d\n",
  767. ret);
  768. }
  769. /* Make sure all DAPM widgets are freed */
  770. snd_soc_dapm_free(&platform->dapm);
  771. soc_cleanup_platform_debugfs(platform);
  772. platform->probed = 0;
  773. list_del(&platform->card_list);
  774. module_put(platform->dev->driver->owner);
  775. return 0;
  776. }
  777. static void soc_remove_codec(struct snd_soc_codec *codec)
  778. {
  779. int err;
  780. if (codec->driver->remove) {
  781. err = codec->driver->remove(codec);
  782. if (err < 0)
  783. dev_err(codec->dev, "ASoC: failed to remove %d\n", err);
  784. }
  785. /* Make sure all DAPM widgets are freed */
  786. snd_soc_dapm_free(&codec->dapm);
  787. soc_cleanup_codec_debugfs(codec);
  788. codec->probed = 0;
  789. list_del(&codec->card_list);
  790. module_put(codec->dev->driver->owner);
  791. }
  792. static void soc_remove_link_dais(struct snd_soc_card *card, int num, int order)
  793. {
  794. struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
  795. struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
  796. int err;
  797. /* unregister the rtd device */
  798. if (rtd->dev_registered) {
  799. device_remove_file(rtd->dev, &dev_attr_pmdown_time);
  800. device_remove_file(rtd->dev, &dev_attr_codec_reg);
  801. device_unregister(rtd->dev);
  802. rtd->dev_registered = 0;
  803. }
  804. /* remove the CODEC DAI */
  805. if (codec_dai && codec_dai->probed &&
  806. codec_dai->driver->remove_order == order) {
  807. if (codec_dai->driver->remove) {
  808. err = codec_dai->driver->remove(codec_dai);
  809. if (err < 0)
  810. dev_err(codec_dai->dev,
  811. "ASoC: failed to remove %s: %d\n",
  812. codec_dai->name, err);
  813. }
  814. codec_dai->probed = 0;
  815. list_del(&codec_dai->card_list);
  816. }
  817. /* remove the cpu_dai */
  818. if (cpu_dai && cpu_dai->probed &&
  819. cpu_dai->driver->remove_order == order) {
  820. if (cpu_dai->driver->remove) {
  821. err = cpu_dai->driver->remove(cpu_dai);
  822. if (err < 0)
  823. dev_err(cpu_dai->dev,
  824. "ASoC: failed to remove %s: %d\n",
  825. cpu_dai->name, err);
  826. }
  827. cpu_dai->probed = 0;
  828. list_del(&cpu_dai->card_list);
  829. if (!cpu_dai->codec) {
  830. snd_soc_dapm_free(&cpu_dai->dapm);
  831. module_put(cpu_dai->dev->driver->owner);
  832. }
  833. }
  834. }
  835. static void soc_remove_link_components(struct snd_soc_card *card, int num,
  836. int order)
  837. {
  838. struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
  839. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  840. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  841. struct snd_soc_platform *platform = rtd->platform;
  842. struct snd_soc_codec *codec;
  843. /* remove the platform */
  844. if (platform && platform->probed &&
  845. platform->driver->remove_order == order) {
  846. soc_remove_platform(platform);
  847. }
  848. /* remove the CODEC-side CODEC */
  849. if (codec_dai) {
  850. codec = codec_dai->codec;
  851. if (codec && codec->probed &&
  852. codec->driver->remove_order == order)
  853. soc_remove_codec(codec);
  854. }
  855. /* remove any CPU-side CODEC */
  856. if (cpu_dai) {
  857. codec = cpu_dai->codec;
  858. if (codec && codec->probed &&
  859. codec->driver->remove_order == order)
  860. soc_remove_codec(codec);
  861. }
  862. }
  863. static void soc_remove_dai_links(struct snd_soc_card *card)
  864. {
  865. int dai, order;
  866. for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
  867. order++) {
  868. for (dai = 0; dai < card->num_rtd; dai++)
  869. soc_remove_link_dais(card, dai, order);
  870. }
  871. for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
  872. order++) {
  873. for (dai = 0; dai < card->num_rtd; dai++)
  874. soc_remove_link_components(card, dai, order);
  875. }
  876. card->num_rtd = 0;
  877. }
  878. static void soc_set_name_prefix(struct snd_soc_card *card,
  879. struct snd_soc_codec *codec)
  880. {
  881. int i;
  882. if (card->codec_conf == NULL)
  883. return;
  884. for (i = 0; i < card->num_configs; i++) {
  885. struct snd_soc_codec_conf *map = &card->codec_conf[i];
  886. if (map->dev_name && !strcmp(codec->name, map->dev_name)) {
  887. codec->name_prefix = map->name_prefix;
  888. break;
  889. }
  890. }
  891. }
  892. static int soc_probe_codec(struct snd_soc_card *card,
  893. struct snd_soc_codec *codec)
  894. {
  895. int ret = 0;
  896. const struct snd_soc_codec_driver *driver = codec->driver;
  897. struct snd_soc_dai *dai;
  898. codec->card = card;
  899. codec->dapm.card = card;
  900. soc_set_name_prefix(card, codec);
  901. if (!try_module_get(codec->dev->driver->owner))
  902. return -ENODEV;
  903. soc_init_codec_debugfs(codec);
  904. if (driver->dapm_widgets)
  905. snd_soc_dapm_new_controls(&codec->dapm, driver->dapm_widgets,
  906. driver->num_dapm_widgets);
  907. /* Create DAPM widgets for each DAI stream */
  908. list_for_each_entry(dai, &dai_list, list) {
  909. if (dai->dev != codec->dev)
  910. continue;
  911. snd_soc_dapm_new_dai_widgets(&codec->dapm, dai);
  912. }
  913. codec->dapm.idle_bias_off = driver->idle_bias_off;
  914. if (driver->probe) {
  915. ret = driver->probe(codec);
  916. if (ret < 0) {
  917. dev_err(codec->dev,
  918. "ASoC: failed to probe CODEC %d\n", ret);
  919. goto err_probe;
  920. }
  921. WARN(codec->dapm.idle_bias_off &&
  922. codec->dapm.bias_level != SND_SOC_BIAS_OFF,
  923. "codec %s can not start from non-off bias with idle_bias_off==1\n",
  924. codec->name);
  925. }
  926. /* If the driver didn't set I/O up try regmap */
  927. if (!codec->write && dev_get_regmap(codec->dev, NULL))
  928. snd_soc_codec_set_cache_io(codec, 0, 0, SND_SOC_REGMAP);
  929. if (driver->controls)
  930. snd_soc_add_codec_controls(codec, driver->controls,
  931. driver->num_controls);
  932. if (driver->dapm_routes)
  933. snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes,
  934. driver->num_dapm_routes);
  935. /* mark codec as probed and add to card codec list */
  936. codec->probed = 1;
  937. list_add(&codec->card_list, &card->codec_dev_list);
  938. list_add(&codec->dapm.list, &card->dapm_list);
  939. return 0;
  940. err_probe:
  941. soc_cleanup_codec_debugfs(codec);
  942. module_put(codec->dev->driver->owner);
  943. return ret;
  944. }
  945. static int soc_probe_platform(struct snd_soc_card *card,
  946. struct snd_soc_platform *platform)
  947. {
  948. int ret = 0;
  949. const struct snd_soc_platform_driver *driver = platform->driver;
  950. struct snd_soc_dai *dai;
  951. platform->card = card;
  952. platform->dapm.card = card;
  953. if (!try_module_get(platform->dev->driver->owner))
  954. return -ENODEV;
  955. soc_init_platform_debugfs(platform);
  956. if (driver->dapm_widgets)
  957. snd_soc_dapm_new_controls(&platform->dapm,
  958. driver->dapm_widgets, driver->num_dapm_widgets);
  959. /* Create DAPM widgets for each DAI stream */
  960. list_for_each_entry(dai, &dai_list, list) {
  961. if (dai->dev != platform->dev)
  962. continue;
  963. snd_soc_dapm_new_dai_widgets(&platform->dapm, dai);
  964. }
  965. platform->dapm.idle_bias_off = 1;
  966. if (driver->probe) {
  967. ret = driver->probe(platform);
  968. if (ret < 0) {
  969. dev_err(platform->dev,
  970. "ASoC: failed to probe platform %d\n", ret);
  971. goto err_probe;
  972. }
  973. }
  974. if (driver->controls)
  975. snd_soc_add_platform_controls(platform, driver->controls,
  976. driver->num_controls);
  977. if (driver->dapm_routes)
  978. snd_soc_dapm_add_routes(&platform->dapm, driver->dapm_routes,
  979. driver->num_dapm_routes);
  980. /* mark platform as probed and add to card platform list */
  981. platform->probed = 1;
  982. list_add(&platform->card_list, &card->platform_dev_list);
  983. list_add(&platform->dapm.list, &card->dapm_list);
  984. return 0;
  985. err_probe:
  986. soc_cleanup_platform_debugfs(platform);
  987. module_put(platform->dev->driver->owner);
  988. return ret;
  989. }
  990. static void rtd_release(struct device *dev)
  991. {
  992. kfree(dev);
  993. }
  994. static int soc_post_component_init(struct snd_soc_card *card,
  995. struct snd_soc_codec *codec,
  996. int num, int dailess)
  997. {
  998. struct snd_soc_dai_link *dai_link = NULL;
  999. struct snd_soc_aux_dev *aux_dev = NULL;
  1000. struct snd_soc_pcm_runtime *rtd;
  1001. const char *temp, *name;
  1002. int ret = 0;
  1003. if (!dailess) {
  1004. dai_link = &card->dai_link[num];
  1005. rtd = &card->rtd[num];
  1006. name = dai_link->name;
  1007. } else {
  1008. aux_dev = &card->aux_dev[num];
  1009. rtd = &card->rtd_aux[num];
  1010. name = aux_dev->name;
  1011. }
  1012. rtd->card = card;
  1013. /* Make sure all DAPM widgets are instantiated */
  1014. snd_soc_dapm_new_widgets(&codec->dapm);
  1015. /* machine controls, routes and widgets are not prefixed */
  1016. temp = codec->name_prefix;
  1017. codec->name_prefix = NULL;
  1018. /* do machine specific initialization */
  1019. if (!dailess && dai_link->init)
  1020. ret = dai_link->init(rtd);
  1021. else if (dailess && aux_dev->init)
  1022. ret = aux_dev->init(&codec->dapm);
  1023. if (ret < 0) {
  1024. dev_err(card->dev, "ASoC: failed to init %s: %d\n", name, ret);
  1025. return ret;
  1026. }
  1027. codec->name_prefix = temp;
  1028. /* register the rtd device */
  1029. rtd->codec = codec;
  1030. rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
  1031. if (!rtd->dev)
  1032. return -ENOMEM;
  1033. device_initialize(rtd->dev);
  1034. rtd->dev->parent = card->dev;
  1035. rtd->dev->release = rtd_release;
  1036. rtd->dev->init_name = name;
  1037. dev_set_drvdata(rtd->dev, rtd);
  1038. mutex_init(&rtd->pcm_mutex);
  1039. INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
  1040. INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
  1041. INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
  1042. INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
  1043. ret = device_add(rtd->dev);
  1044. if (ret < 0) {
  1045. /* calling put_device() here to free the rtd->dev */
  1046. put_device(rtd->dev);
  1047. dev_err(card->dev,
  1048. "ASoC: failed to register runtime device: %d\n", ret);
  1049. return ret;
  1050. }
  1051. rtd->dev_registered = 1;
  1052. /* add DAPM sysfs entries for this codec */
  1053. ret = snd_soc_dapm_sys_add(rtd->dev);
  1054. if (ret < 0)
  1055. dev_err(codec->dev,
  1056. "ASoC: failed to add codec dapm sysfs entries: %d\n", ret);
  1057. /* add codec sysfs entries */
  1058. ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
  1059. if (ret < 0)
  1060. dev_err(codec->dev,
  1061. "ASoC: failed to add codec sysfs files: %d\n", ret);
  1062. #ifdef CONFIG_DEBUG_FS
  1063. /* add DPCM sysfs entries */
  1064. if (!dailess && !dai_link->dynamic)
  1065. goto out;
  1066. ret = soc_dpcm_debugfs_add(rtd);
  1067. if (ret < 0)
  1068. dev_err(rtd->dev, "ASoC: failed to add dpcm sysfs entries: %d\n", ret);
  1069. out:
  1070. #endif
  1071. return 0;
  1072. }
  1073. static int soc_probe_link_components(struct snd_soc_card *card, int num,
  1074. int order)
  1075. {
  1076. struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
  1077. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  1078. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  1079. struct snd_soc_platform *platform = rtd->platform;
  1080. int ret;
  1081. /* probe the CPU-side component, if it is a CODEC */
  1082. if (cpu_dai->codec &&
  1083. !cpu_dai->codec->probed &&
  1084. cpu_dai->codec->driver->probe_order == order) {
  1085. ret = soc_probe_codec(card, cpu_dai->codec);
  1086. if (ret < 0)
  1087. return ret;
  1088. }
  1089. /* probe the CODEC-side component */
  1090. if (!codec_dai->codec->probed &&
  1091. codec_dai->codec->driver->probe_order == order) {
  1092. ret = soc_probe_codec(card, codec_dai->codec);
  1093. if (ret < 0)
  1094. return ret;
  1095. }
  1096. /* probe the platform */
  1097. if (!platform->probed &&
  1098. platform->driver->probe_order == order) {
  1099. ret = soc_probe_platform(card, platform);
  1100. if (ret < 0)
  1101. return ret;
  1102. }
  1103. return 0;
  1104. }
  1105. static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order)
  1106. {
  1107. struct snd_soc_dai_link *dai_link = &card->dai_link[num];
  1108. struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
  1109. struct snd_soc_codec *codec = rtd->codec;
  1110. struct snd_soc_platform *platform = rtd->platform;
  1111. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  1112. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  1113. struct snd_soc_dapm_widget *play_w, *capture_w;
  1114. int ret;
  1115. dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
  1116. card->name, num, order);
  1117. /* config components */
  1118. cpu_dai->platform = platform;
  1119. codec_dai->card = card;
  1120. cpu_dai->card = card;
  1121. /* set default power off timeout */
  1122. rtd->pmdown_time = pmdown_time;
  1123. /* probe the cpu_dai */
  1124. if (!cpu_dai->probed &&
  1125. cpu_dai->driver->probe_order == order) {
  1126. if (!cpu_dai->codec) {
  1127. cpu_dai->dapm.card = card;
  1128. if (!try_module_get(cpu_dai->dev->driver->owner))
  1129. return -ENODEV;
  1130. list_add(&cpu_dai->dapm.list, &card->dapm_list);
  1131. snd_soc_dapm_new_dai_widgets(&cpu_dai->dapm, cpu_dai);
  1132. }
  1133. if (cpu_dai->driver->probe) {
  1134. ret = cpu_dai->driver->probe(cpu_dai);
  1135. if (ret < 0) {
  1136. dev_err(cpu_dai->dev,
  1137. "ASoC: failed to probe CPU DAI %s: %d\n",
  1138. cpu_dai->name, ret);
  1139. module_put(cpu_dai->dev->driver->owner);
  1140. return ret;
  1141. }
  1142. }
  1143. cpu_dai->probed = 1;
  1144. /* mark cpu_dai as probed and add to card dai list */
  1145. list_add(&cpu_dai->card_list, &card->dai_dev_list);
  1146. }
  1147. /* probe the CODEC DAI */
  1148. if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
  1149. if (codec_dai->driver->probe) {
  1150. ret = codec_dai->driver->probe(codec_dai);
  1151. if (ret < 0) {
  1152. dev_err(codec_dai->dev,
  1153. "ASoC: failed to probe CODEC DAI %s: %d\n",
  1154. codec_dai->name, ret);
  1155. return ret;
  1156. }
  1157. }
  1158. /* mark codec_dai as probed and add to card dai list */
  1159. codec_dai->probed = 1;
  1160. list_add(&codec_dai->card_list, &card->dai_dev_list);
  1161. }
  1162. /* complete DAI probe during last probe */
  1163. if (order != SND_SOC_COMP_ORDER_LAST)
  1164. return 0;
  1165. ret = soc_post_component_init(card, codec, num, 0);
  1166. if (ret)
  1167. return ret;
  1168. ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
  1169. if (ret < 0)
  1170. dev_warn(rtd->dev, "ASoC: failed to add pmdown_time sysfs: %d\n",
  1171. ret);
  1172. if (cpu_dai->driver->compress_dai) {
  1173. /*create compress_device"*/
  1174. ret = soc_new_compress(rtd, num);
  1175. if (ret < 0) {
  1176. dev_err(card->dev, "ASoC: can't create compress %s\n",
  1177. dai_link->stream_name);
  1178. return ret;
  1179. }
  1180. } else {
  1181. if (!dai_link->params) {
  1182. /* create the pcm */
  1183. ret = soc_new_pcm(rtd, num);
  1184. if (ret < 0) {
  1185. dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
  1186. dai_link->stream_name, ret);
  1187. return ret;
  1188. }
  1189. } else {
  1190. INIT_DELAYED_WORK(&rtd->delayed_work,
  1191. codec2codec_close_delayed_work);
  1192. /* link the DAI widgets */
  1193. play_w = codec_dai->playback_widget;
  1194. capture_w = cpu_dai->capture_widget;
  1195. if (play_w && capture_w) {
  1196. ret = snd_soc_dapm_new_pcm(card, dai_link->params,
  1197. capture_w, play_w);
  1198. if (ret != 0) {
  1199. dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
  1200. play_w->name, capture_w->name, ret);
  1201. return ret;
  1202. }
  1203. }
  1204. play_w = cpu_dai->playback_widget;
  1205. capture_w = codec_dai->capture_widget;
  1206. if (play_w && capture_w) {
  1207. ret = snd_soc_dapm_new_pcm(card, dai_link->params,
  1208. capture_w, play_w);
  1209. if (ret != 0) {
  1210. dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
  1211. play_w->name, capture_w->name, ret);
  1212. return ret;
  1213. }
  1214. }
  1215. }
  1216. }
  1217. /* add platform data for AC97 devices */
  1218. if (rtd->codec_dai->driver->ac97_control)
  1219. snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
  1220. return 0;
  1221. }
  1222. #ifdef CONFIG_SND_SOC_AC97_BUS
  1223. static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
  1224. {
  1225. int ret;
  1226. /* Only instantiate AC97 if not already done by the adaptor
  1227. * for the generic AC97 subsystem.
  1228. */
  1229. if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
  1230. /*
  1231. * It is possible that the AC97 device is already registered to
  1232. * the device subsystem. This happens when the device is created
  1233. * via snd_ac97_mixer(). Currently only SoC codec that does so
  1234. * is the generic AC97 glue but others migh emerge.
  1235. *
  1236. * In those cases we don't try to register the device again.
  1237. */
  1238. if (!rtd->codec->ac97_created)
  1239. return 0;
  1240. ret = soc_ac97_dev_register(rtd->codec);
  1241. if (ret < 0) {
  1242. dev_err(rtd->codec->dev,
  1243. "ASoC: AC97 device register failed: %d\n", ret);
  1244. return ret;
  1245. }
  1246. rtd->codec->ac97_registered = 1;
  1247. }
  1248. return 0;
  1249. }
  1250. static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
  1251. {
  1252. if (codec->ac97_registered) {
  1253. soc_ac97_dev_unregister(codec);
  1254. codec->ac97_registered = 0;
  1255. }
  1256. }
  1257. #endif
  1258. static int soc_check_aux_dev(struct snd_soc_card *card, int num)
  1259. {
  1260. struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
  1261. struct snd_soc_codec *codec;
  1262. /* find CODEC from registered CODECs*/
  1263. list_for_each_entry(codec, &codec_list, list) {
  1264. if (!strcmp(codec->name, aux_dev->codec_name))
  1265. return 0;
  1266. }
  1267. dev_err(card->dev, "ASoC: %s not registered\n", aux_dev->codec_name);
  1268. return -EPROBE_DEFER;
  1269. }
  1270. static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
  1271. {
  1272. struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
  1273. struct snd_soc_codec *codec;
  1274. int ret = -ENODEV;
  1275. /* find CODEC from registered CODECs*/
  1276. list_for_each_entry(codec, &codec_list, list) {
  1277. if (!strcmp(codec->name, aux_dev->codec_name)) {
  1278. if (codec->probed) {
  1279. dev_err(codec->dev,
  1280. "ASoC: codec already probed");
  1281. ret = -EBUSY;
  1282. goto out;
  1283. }
  1284. goto found;
  1285. }
  1286. }
  1287. /* codec not found */
  1288. dev_err(card->dev, "ASoC: codec %s not found", aux_dev->codec_name);
  1289. return -EPROBE_DEFER;
  1290. found:
  1291. ret = soc_probe_codec(card, codec);
  1292. if (ret < 0)
  1293. return ret;
  1294. ret = soc_post_component_init(card, codec, num, 1);
  1295. out:
  1296. return ret;
  1297. }
  1298. static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
  1299. {
  1300. struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
  1301. struct snd_soc_codec *codec = rtd->codec;
  1302. /* unregister the rtd device */
  1303. if (rtd->dev_registered) {
  1304. device_remove_file(rtd->dev, &dev_attr_codec_reg);
  1305. device_unregister(rtd->dev);
  1306. rtd->dev_registered = 0;
  1307. }
  1308. if (codec && codec->probed)
  1309. soc_remove_codec(codec);
  1310. }
  1311. static int snd_soc_init_codec_cache(struct snd_soc_codec *codec,
  1312. enum snd_soc_compress_type compress_type)
  1313. {
  1314. int ret;
  1315. if (codec->cache_init)
  1316. return 0;
  1317. /* override the compress_type if necessary */
  1318. if (compress_type && codec->compress_type != compress_type)
  1319. codec->compress_type = compress_type;
  1320. ret = snd_soc_cache_init(codec);
  1321. if (ret < 0) {
  1322. dev_err(codec->dev,
  1323. "ASoC: Failed to set cache compression type: %d\n",
  1324. ret);
  1325. return ret;
  1326. }
  1327. codec->cache_init = 1;
  1328. return 0;
  1329. }
  1330. static int snd_soc_instantiate_card(struct snd_soc_card *card)
  1331. {
  1332. struct snd_soc_codec *codec;
  1333. struct snd_soc_codec_conf *codec_conf;
  1334. enum snd_soc_compress_type compress_type;
  1335. struct snd_soc_dai_link *dai_link;
  1336. int ret, i, order, dai_fmt;
  1337. mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
  1338. /* bind DAIs */
  1339. for (i = 0; i < card->num_links; i++) {
  1340. ret = soc_bind_dai_link(card, i);
  1341. if (ret != 0)
  1342. goto base_error;
  1343. }
  1344. /* check aux_devs too */
  1345. for (i = 0; i < card->num_aux_devs; i++) {
  1346. ret = soc_check_aux_dev(card, i);
  1347. if (ret != 0)
  1348. goto base_error;
  1349. }
  1350. /* initialize the register cache for each available codec */
  1351. list_for_each_entry(codec, &codec_list, list) {
  1352. if (codec->cache_init)
  1353. continue;
  1354. /* by default we don't override the compress_type */
  1355. compress_type = 0;
  1356. /* check to see if we need to override the compress_type */
  1357. for (i = 0; i < card->num_configs; ++i) {
  1358. codec_conf = &card->codec_conf[i];
  1359. if (!strcmp(codec->name, codec_conf->dev_name)) {
  1360. compress_type = codec_conf->compress_type;
  1361. if (compress_type && compress_type
  1362. != codec->compress_type)
  1363. break;
  1364. }
  1365. }
  1366. ret = snd_soc_init_codec_cache(codec, compress_type);
  1367. if (ret < 0)
  1368. goto base_error;
  1369. }
  1370. /* card bind complete so register a sound card */
  1371. ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
  1372. card->owner, 0, &card->snd_card);
  1373. if (ret < 0) {
  1374. dev_err(card->dev,
  1375. "ASoC: can't create sound card for card %s: %d\n",
  1376. card->name, ret);
  1377. goto base_error;
  1378. }
  1379. card->snd_card->dev = card->dev;
  1380. card->dapm.bias_level = SND_SOC_BIAS_OFF;
  1381. card->dapm.dev = card->dev;
  1382. card->dapm.card = card;
  1383. list_add(&card->dapm.list, &card->dapm_list);
  1384. #ifdef CONFIG_DEBUG_FS
  1385. snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
  1386. #endif
  1387. #ifdef CONFIG_PM_SLEEP
  1388. /* deferred resume work */
  1389. INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
  1390. #endif
  1391. if (card->dapm_widgets)
  1392. snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
  1393. card->num_dapm_widgets);
  1394. /* initialise the sound card only once */
  1395. if (card->probe) {
  1396. ret = card->probe(card);
  1397. if (ret < 0)
  1398. goto card_probe_error;
  1399. }
  1400. /* probe all components used by DAI links on this card */
  1401. for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
  1402. order++) {
  1403. for (i = 0; i < card->num_links; i++) {
  1404. ret = soc_probe_link_components(card, i, order);
  1405. if (ret < 0) {
  1406. dev_err(card->dev,
  1407. "ASoC: failed to instantiate card %d\n",
  1408. ret);
  1409. goto probe_dai_err;
  1410. }
  1411. }
  1412. }
  1413. /* probe all DAI links on this card */
  1414. for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
  1415. order++) {
  1416. for (i = 0; i < card->num_links; i++) {
  1417. ret = soc_probe_link_dais(card, i, order);
  1418. if (ret < 0) {
  1419. dev_err(card->dev,
  1420. "ASoC: failed to instantiate card %d\n",
  1421. ret);
  1422. goto probe_dai_err;
  1423. }
  1424. }
  1425. }
  1426. for (i = 0; i < card->num_aux_devs; i++) {
  1427. ret = soc_probe_aux_dev(card, i);
  1428. if (ret < 0) {
  1429. dev_err(card->dev,
  1430. "ASoC: failed to add auxiliary devices %d\n",
  1431. ret);
  1432. goto probe_aux_dev_err;
  1433. }
  1434. }
  1435. snd_soc_dapm_link_dai_widgets(card);
  1436. if (card->controls)
  1437. snd_soc_add_card_controls(card, card->controls, card->num_controls);
  1438. if (card->dapm_routes)
  1439. snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
  1440. card->num_dapm_routes);
  1441. snd_soc_dapm_new_widgets(&card->dapm);
  1442. for (i = 0; i < card->num_links; i++) {
  1443. dai_link = &card->dai_link[i];
  1444. dai_fmt = dai_link->dai_fmt;
  1445. if (dai_fmt) {
  1446. ret = snd_soc_dai_set_fmt(card->rtd[i].codec_dai,
  1447. dai_fmt);
  1448. if (ret != 0 && ret != -ENOTSUPP)
  1449. dev_warn(card->rtd[i].codec_dai->dev,
  1450. "ASoC: Failed to set DAI format: %d\n",
  1451. ret);
  1452. }
  1453. /* If this is a regular CPU link there will be a platform */
  1454. if (dai_fmt &&
  1455. (dai_link->platform_name || dai_link->platform_of_node)) {
  1456. ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
  1457. dai_fmt);
  1458. if (ret != 0 && ret != -ENOTSUPP)
  1459. dev_warn(card->rtd[i].cpu_dai->dev,
  1460. "ASoC: Failed to set DAI format: %d\n",
  1461. ret);
  1462. } else if (dai_fmt) {
  1463. /* Flip the polarity for the "CPU" end */
  1464. dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
  1465. switch (dai_link->dai_fmt &
  1466. SND_SOC_DAIFMT_MASTER_MASK) {
  1467. case SND_SOC_DAIFMT_CBM_CFM:
  1468. dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
  1469. break;
  1470. case SND_SOC_DAIFMT_CBM_CFS:
  1471. dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
  1472. break;
  1473. case SND_SOC_DAIFMT_CBS_CFM:
  1474. dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
  1475. break;
  1476. case SND_SOC_DAIFMT_CBS_CFS:
  1477. dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
  1478. break;
  1479. }
  1480. ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
  1481. dai_fmt);
  1482. if (ret != 0 && ret != -ENOTSUPP)
  1483. dev_warn(card->rtd[i].cpu_dai->dev,
  1484. "ASoC: Failed to set DAI format: %d\n",
  1485. ret);
  1486. }
  1487. }
  1488. snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
  1489. "%s", card->name);
  1490. snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
  1491. "%s", card->long_name ? card->long_name : card->name);
  1492. snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
  1493. "%s", card->driver_name ? card->driver_name : card->name);
  1494. for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
  1495. switch (card->snd_card->driver[i]) {
  1496. case '_':
  1497. case '-':
  1498. case '\0':
  1499. break;
  1500. default:
  1501. if (!isalnum(card->snd_card->driver[i]))
  1502. card->snd_card->driver[i] = '_';
  1503. break;
  1504. }
  1505. }
  1506. if (card->late_probe) {
  1507. ret = card->late_probe(card);
  1508. if (ret < 0) {
  1509. dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
  1510. card->name, ret);
  1511. goto probe_aux_dev_err;
  1512. }
  1513. }
  1514. snd_soc_dapm_new_widgets(&card->dapm);
  1515. if (card->fully_routed)
  1516. list_for_each_entry(codec, &card->codec_dev_list, card_list)
  1517. snd_soc_dapm_auto_nc_codec_pins(codec);
  1518. ret = snd_card_register(card->snd_card);
  1519. if (ret < 0) {
  1520. dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
  1521. ret);
  1522. goto probe_aux_dev_err;
  1523. }
  1524. #ifdef CONFIG_SND_SOC_AC97_BUS
  1525. /* register any AC97 codecs */
  1526. for (i = 0; i < card->num_rtd; i++) {
  1527. ret = soc_register_ac97_dai_link(&card->rtd[i]);
  1528. if (ret < 0) {
  1529. dev_err(card->dev,
  1530. "ASoC: failed to register AC97: %d\n", ret);
  1531. while (--i >= 0)
  1532. soc_unregister_ac97_dai_link(card->rtd[i].codec);
  1533. goto probe_aux_dev_err;
  1534. }
  1535. }
  1536. #endif
  1537. card->instantiated = 1;
  1538. snd_soc_dapm_sync(&card->dapm);
  1539. mutex_unlock(&card->mutex);
  1540. return 0;
  1541. probe_aux_dev_err:
  1542. for (i = 0; i < card->num_aux_devs; i++)
  1543. soc_remove_aux_dev(card, i);
  1544. probe_dai_err:
  1545. soc_remove_dai_links(card);
  1546. card_probe_error:
  1547. if (card->remove)
  1548. card->remove(card);
  1549. snd_card_free(card->snd_card);
  1550. base_error:
  1551. mutex_unlock(&card->mutex);
  1552. return ret;
  1553. }
  1554. /* probes a new socdev */
  1555. static int soc_probe(struct platform_device *pdev)
  1556. {
  1557. struct snd_soc_card *card = platform_get_drvdata(pdev);
  1558. /*
  1559. * no card, so machine driver should be registering card
  1560. * we should not be here in that case so ret error
  1561. */
  1562. if (!card)
  1563. return -EINVAL;
  1564. dev_warn(&pdev->dev,
  1565. "ASoC: machine %s should use snd_soc_register_card()\n",
  1566. card->name);
  1567. /* Bodge while we unpick instantiation */
  1568. card->dev = &pdev->dev;
  1569. return snd_soc_register_card(card);
  1570. }
  1571. static int soc_cleanup_card_resources(struct snd_soc_card *card)
  1572. {
  1573. int i;
  1574. /* make sure any delayed work runs */
  1575. for (i = 0; i < card->num_rtd; i++) {
  1576. struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
  1577. flush_delayed_work(&rtd->delayed_work);
  1578. }
  1579. /* remove auxiliary devices */
  1580. for (i = 0; i < card->num_aux_devs; i++)
  1581. soc_remove_aux_dev(card, i);
  1582. /* remove and free each DAI */
  1583. soc_remove_dai_links(card);
  1584. soc_cleanup_card_debugfs(card);
  1585. /* remove the card */
  1586. if (card->remove)
  1587. card->remove(card);
  1588. snd_soc_dapm_free(&card->dapm);
  1589. snd_card_free(card->snd_card);
  1590. return 0;
  1591. }
  1592. /* removes a socdev */
  1593. static int soc_remove(struct platform_device *pdev)
  1594. {
  1595. struct snd_soc_card *card = platform_get_drvdata(pdev);
  1596. snd_soc_unregister_card(card);
  1597. return 0;
  1598. }
  1599. int snd_soc_poweroff(struct device *dev)
  1600. {
  1601. struct snd_soc_card *card = dev_get_drvdata(dev);
  1602. int i;
  1603. if (!card->instantiated)
  1604. return 0;
  1605. /* Flush out pmdown_time work - we actually do want to run it
  1606. * now, we're shutting down so no imminent restart. */
  1607. for (i = 0; i < card->num_rtd; i++) {
  1608. struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
  1609. flush_delayed_work(&rtd->delayed_work);
  1610. }
  1611. snd_soc_dapm_shutdown(card);
  1612. return 0;
  1613. }
  1614. EXPORT_SYMBOL_GPL(snd_soc_poweroff);
  1615. const struct dev_pm_ops snd_soc_pm_ops = {
  1616. .suspend = snd_soc_suspend,
  1617. .resume = snd_soc_resume,
  1618. .freeze = snd_soc_suspend,
  1619. .thaw = snd_soc_resume,
  1620. .poweroff = snd_soc_poweroff,
  1621. .restore = snd_soc_resume,
  1622. };
  1623. EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
  1624. /* ASoC platform driver */
  1625. static struct platform_driver soc_driver = {
  1626. .driver = {
  1627. .name = "soc-audio",
  1628. .owner = THIS_MODULE,
  1629. .pm = &snd_soc_pm_ops,
  1630. },
  1631. .probe = soc_probe,
  1632. .remove = soc_remove,
  1633. };
  1634. /**
  1635. * snd_soc_codec_volatile_register: Report if a register is volatile.
  1636. *
  1637. * @codec: CODEC to query.
  1638. * @reg: Register to query.
  1639. *
  1640. * Boolean function indiciating if a CODEC register is volatile.
  1641. */
  1642. int snd_soc_codec_volatile_register(struct snd_soc_codec *codec,
  1643. unsigned int reg)
  1644. {
  1645. if (codec->volatile_register)
  1646. return codec->volatile_register(codec, reg);
  1647. else
  1648. return 0;
  1649. }
  1650. EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
  1651. /**
  1652. * snd_soc_codec_readable_register: Report if a register is readable.
  1653. *
  1654. * @codec: CODEC to query.
  1655. * @reg: Register to query.
  1656. *
  1657. * Boolean function indicating if a CODEC register is readable.
  1658. */
  1659. int snd_soc_codec_readable_register(struct snd_soc_codec *codec,
  1660. unsigned int reg)
  1661. {
  1662. if (codec->readable_register)
  1663. return codec->readable_register(codec, reg);
  1664. else
  1665. return 1;
  1666. }
  1667. EXPORT_SYMBOL_GPL(snd_soc_codec_readable_register);
  1668. /**
  1669. * snd_soc_codec_writable_register: Report if a register is writable.
  1670. *
  1671. * @codec: CODEC to query.
  1672. * @reg: Register to query.
  1673. *
  1674. * Boolean function indicating if a CODEC register is writable.
  1675. */
  1676. int snd_soc_codec_writable_register(struct snd_soc_codec *codec,
  1677. unsigned int reg)
  1678. {
  1679. if (codec->writable_register)
  1680. return codec->writable_register(codec, reg);
  1681. else
  1682. return 1;
  1683. }
  1684. EXPORT_SYMBOL_GPL(snd_soc_codec_writable_register);
  1685. int snd_soc_platform_read(struct snd_soc_platform *platform,
  1686. unsigned int reg)
  1687. {
  1688. unsigned int ret;
  1689. if (!platform->driver->read) {
  1690. dev_err(platform->dev, "ASoC: platform has no read back\n");
  1691. return -1;
  1692. }
  1693. ret = platform->driver->read(platform, reg);
  1694. dev_dbg(platform->dev, "read %x => %x\n", reg, ret);
  1695. trace_snd_soc_preg_read(platform, reg, ret);
  1696. return ret;
  1697. }
  1698. EXPORT_SYMBOL_GPL(snd_soc_platform_read);
  1699. int snd_soc_platform_write(struct snd_soc_platform *platform,
  1700. unsigned int reg, unsigned int val)
  1701. {
  1702. if (!platform->driver->write) {
  1703. dev_err(platform->dev, "ASoC: platform has no write back\n");
  1704. return -1;
  1705. }
  1706. dev_dbg(platform->dev, "write %x = %x\n", reg, val);
  1707. trace_snd_soc_preg_write(platform, reg, val);
  1708. return platform->driver->write(platform, reg, val);
  1709. }
  1710. EXPORT_SYMBOL_GPL(snd_soc_platform_write);
  1711. /**
  1712. * snd_soc_new_ac97_codec - initailise AC97 device
  1713. * @codec: audio codec
  1714. * @ops: AC97 bus operations
  1715. * @num: AC97 codec number
  1716. *
  1717. * Initialises AC97 codec resources for use by ad-hoc devices only.
  1718. */
  1719. int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
  1720. struct snd_ac97_bus_ops *ops, int num)
  1721. {
  1722. mutex_lock(&codec->mutex);
  1723. codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
  1724. if (codec->ac97 == NULL) {
  1725. mutex_unlock(&codec->mutex);
  1726. return -ENOMEM;
  1727. }
  1728. codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
  1729. if (codec->ac97->bus == NULL) {
  1730. kfree(codec->ac97);
  1731. codec->ac97 = NULL;
  1732. mutex_unlock(&codec->mutex);
  1733. return -ENOMEM;
  1734. }
  1735. codec->ac97->bus->ops = ops;
  1736. codec->ac97->num = num;
  1737. /*
  1738. * Mark the AC97 device to be created by us. This way we ensure that the
  1739. * device will be registered with the device subsystem later on.
  1740. */
  1741. codec->ac97_created = 1;
  1742. mutex_unlock(&codec->mutex);
  1743. return 0;
  1744. }
  1745. EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
  1746. struct snd_ac97_bus_ops *soc_ac97_ops;
  1747. EXPORT_SYMBOL_GPL(soc_ac97_ops);
  1748. int snd_soc_set_ac97_ops(struct snd_ac97_bus_ops *ops)
  1749. {
  1750. if (ops == soc_ac97_ops)
  1751. return 0;
  1752. if (soc_ac97_ops && ops)
  1753. return -EBUSY;
  1754. soc_ac97_ops = ops;
  1755. return 0;
  1756. }
  1757. EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops);
  1758. /**
  1759. * snd_soc_free_ac97_codec - free AC97 codec device
  1760. * @codec: audio codec
  1761. *
  1762. * Frees AC97 codec device resources.
  1763. */
  1764. void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
  1765. {
  1766. mutex_lock(&codec->mutex);
  1767. #ifdef CONFIG_SND_SOC_AC97_BUS
  1768. soc_unregister_ac97_dai_link(codec);
  1769. #endif
  1770. kfree(codec->ac97->bus);
  1771. kfree(codec->ac97);
  1772. codec->ac97 = NULL;
  1773. codec->ac97_created = 0;
  1774. mutex_unlock(&codec->mutex);
  1775. }
  1776. EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
  1777. unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
  1778. {
  1779. unsigned int ret;
  1780. ret = codec->read(codec, reg);
  1781. dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
  1782. trace_snd_soc_reg_read(codec, reg, ret);
  1783. return ret;
  1784. }
  1785. EXPORT_SYMBOL_GPL(snd_soc_read);
  1786. unsigned int snd_soc_write(struct snd_soc_codec *codec,
  1787. unsigned int reg, unsigned int val)
  1788. {
  1789. dev_dbg(codec->dev, "write %x = %x\n", reg, val);
  1790. trace_snd_soc_reg_write(codec, reg, val);
  1791. return codec->write(codec, reg, val);
  1792. }
  1793. EXPORT_SYMBOL_GPL(snd_soc_write);
  1794. unsigned int snd_soc_bulk_write_raw(struct snd_soc_codec *codec,
  1795. unsigned int reg, const void *data, size_t len)
  1796. {
  1797. return codec->bulk_write_raw(codec, reg, data, len);
  1798. }
  1799. EXPORT_SYMBOL_GPL(snd_soc_bulk_write_raw);
  1800. /**
  1801. * snd_soc_update_bits - update codec register bits
  1802. * @codec: audio codec
  1803. * @reg: codec register
  1804. * @mask: register mask
  1805. * @value: new value
  1806. *
  1807. * Writes new register value.
  1808. *
  1809. * Returns 1 for change, 0 for no change, or negative error code.
  1810. */
  1811. int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
  1812. unsigned int mask, unsigned int value)
  1813. {
  1814. bool change;
  1815. unsigned int old, new;
  1816. int ret;
  1817. if (codec->using_regmap) {
  1818. ret = regmap_update_bits_check(codec->control_data, reg,
  1819. mask, value, &change);
  1820. } else {
  1821. ret = snd_soc_read(codec, reg);
  1822. if (ret < 0)
  1823. return ret;
  1824. old = ret;
  1825. new = (old & ~mask) | (value & mask);
  1826. change = old != new;
  1827. if (change)
  1828. ret = snd_soc_write(codec, reg, new);
  1829. }
  1830. if (ret < 0)
  1831. return ret;
  1832. return change;
  1833. }
  1834. EXPORT_SYMBOL_GPL(snd_soc_update_bits);
  1835. /**
  1836. * snd_soc_update_bits_locked - update codec register bits
  1837. * @codec: audio codec
  1838. * @reg: codec register
  1839. * @mask: register mask
  1840. * @value: new value
  1841. *
  1842. * Writes new register value, and takes the codec mutex.
  1843. *
  1844. * Returns 1 for change else 0.
  1845. */
  1846. int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
  1847. unsigned short reg, unsigned int mask,
  1848. unsigned int value)
  1849. {
  1850. int change;
  1851. mutex_lock(&codec->mutex);
  1852. change = snd_soc_update_bits(codec, reg, mask, value);
  1853. mutex_unlock(&codec->mutex);
  1854. return change;
  1855. }
  1856. EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
  1857. /**
  1858. * snd_soc_test_bits - test register for change
  1859. * @codec: audio codec
  1860. * @reg: codec register
  1861. * @mask: register mask
  1862. * @value: new value
  1863. *
  1864. * Tests a register with a new value and checks if the new value is
  1865. * different from the old value.
  1866. *
  1867. * Returns 1 for change else 0.
  1868. */
  1869. int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
  1870. unsigned int mask, unsigned int value)
  1871. {
  1872. int change;
  1873. unsigned int old, new;
  1874. old = snd_soc_read(codec, reg);
  1875. new = (old & ~mask) | value;
  1876. change = old != new;
  1877. return change;
  1878. }
  1879. EXPORT_SYMBOL_GPL(snd_soc_test_bits);
  1880. /**
  1881. * snd_soc_cnew - create new control
  1882. * @_template: control template
  1883. * @data: control private data
  1884. * @long_name: control long name
  1885. * @prefix: control name prefix
  1886. *
  1887. * Create a new mixer control from a template control.
  1888. *
  1889. * Returns 0 for success, else error.
  1890. */
  1891. struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
  1892. void *data, const char *long_name,
  1893. const char *prefix)
  1894. {
  1895. struct snd_kcontrol_new template;
  1896. struct snd_kcontrol *kcontrol;
  1897. char *name = NULL;
  1898. memcpy(&template, _template, sizeof(template));
  1899. template.index = 0;
  1900. if (!long_name)
  1901. long_name = template.name;
  1902. if (prefix) {
  1903. name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
  1904. if (!name)
  1905. return NULL;
  1906. template.name = name;
  1907. } else {
  1908. template.name = long_name;
  1909. }
  1910. kcontrol = snd_ctl_new1(&template, data);
  1911. kfree(name);
  1912. return kcontrol;
  1913. }
  1914. EXPORT_SYMBOL_GPL(snd_soc_cnew);
  1915. static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
  1916. const struct snd_kcontrol_new *controls, int num_controls,
  1917. const char *prefix, void *data)
  1918. {
  1919. int err, i;
  1920. for (i = 0; i < num_controls; i++) {
  1921. const struct snd_kcontrol_new *control = &controls[i];
  1922. err = snd_ctl_add(card, snd_soc_cnew(control, data,
  1923. control->name, prefix));
  1924. if (err < 0) {
  1925. dev_err(dev, "ASoC: Failed to add %s: %d\n",
  1926. control->name, err);
  1927. return err;
  1928. }
  1929. }
  1930. return 0;
  1931. }
  1932. /**
  1933. * snd_soc_add_codec_controls - add an array of controls to a codec.
  1934. * Convenience function to add a list of controls. Many codecs were
  1935. * duplicating this code.
  1936. *
  1937. * @codec: codec to add controls to
  1938. * @controls: array of controls to add
  1939. * @num_controls: number of elements in the array
  1940. *
  1941. * Return 0 for success, else error.
  1942. */
  1943. int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
  1944. const struct snd_kcontrol_new *controls, int num_controls)
  1945. {
  1946. struct snd_card *card = codec->card->snd_card;
  1947. return snd_soc_add_controls(card, codec->dev, controls, num_controls,
  1948. codec->name_prefix, codec);
  1949. }
  1950. EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
  1951. /**
  1952. * snd_soc_add_platform_controls - add an array of controls to a platform.
  1953. * Convenience function to add a list of controls.
  1954. *
  1955. * @platform: platform to add controls to
  1956. * @controls: array of controls to add
  1957. * @num_controls: number of elements in the array
  1958. *
  1959. * Return 0 for success, else error.
  1960. */
  1961. int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
  1962. const struct snd_kcontrol_new *controls, int num_controls)
  1963. {
  1964. struct snd_card *card = platform->card->snd_card;
  1965. return snd_soc_add_controls(card, platform->dev, controls, num_controls,
  1966. NULL, platform);
  1967. }
  1968. EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
  1969. /**
  1970. * snd_soc_add_card_controls - add an array of controls to a SoC card.
  1971. * Convenience function to add a list of controls.
  1972. *
  1973. * @soc_card: SoC card to add controls to
  1974. * @controls: array of controls to add
  1975. * @num_controls: number of elements in the array
  1976. *
  1977. * Return 0 for success, else error.
  1978. */
  1979. int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
  1980. const struct snd_kcontrol_new *controls, int num_controls)
  1981. {
  1982. struct snd_card *card = soc_card->snd_card;
  1983. return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
  1984. NULL, soc_card);
  1985. }
  1986. EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
  1987. /**
  1988. * snd_soc_add_dai_controls - add an array of controls to a DAI.
  1989. * Convienience function to add a list of controls.
  1990. *
  1991. * @dai: DAI to add controls to
  1992. * @controls: array of controls to add
  1993. * @num_controls: number of elements in the array
  1994. *
  1995. * Return 0 for success, else error.
  1996. */
  1997. int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
  1998. const struct snd_kcontrol_new *controls, int num_controls)
  1999. {
  2000. struct snd_card *card = dai->card->snd_card;
  2001. return snd_soc_add_controls(card, dai->dev, controls, num_controls,
  2002. NULL, dai);
  2003. }
  2004. EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
  2005. /**
  2006. * snd_soc_info_enum_double - enumerated double mixer info callback
  2007. * @kcontrol: mixer control
  2008. * @uinfo: control element information
  2009. *
  2010. * Callback to provide information about a double enumerated
  2011. * mixer control.
  2012. *
  2013. * Returns 0 for success.
  2014. */
  2015. int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
  2016. struct snd_ctl_elem_info *uinfo)
  2017. {
  2018. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  2019. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  2020. uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
  2021. uinfo->value.enumerated.items = e->max;
  2022. if (uinfo->value.enumerated.item > e->max - 1)
  2023. uinfo->value.enumerated.item = e->max - 1;
  2024. strcpy(uinfo->value.enumerated.name,
  2025. e->texts[uinfo->value.enumerated.item]);
  2026. return 0;
  2027. }
  2028. EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
  2029. /**
  2030. * snd_soc_get_enum_double - enumerated double mixer get callback
  2031. * @kcontrol: mixer control
  2032. * @ucontrol: control element information
  2033. *
  2034. * Callback to get the value of a double enumerated mixer.
  2035. *
  2036. * Returns 0 for success.
  2037. */
  2038. int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
  2039. struct snd_ctl_elem_value *ucontrol)
  2040. {
  2041. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2042. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  2043. unsigned int val;
  2044. val = snd_soc_read(codec, e->reg);
  2045. ucontrol->value.enumerated.item[0]
  2046. = (val >> e->shift_l) & e->mask;
  2047. if (e->shift_l != e->shift_r)
  2048. ucontrol->value.enumerated.item[1] =
  2049. (val >> e->shift_r) & e->mask;
  2050. return 0;
  2051. }
  2052. EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
  2053. /**
  2054. * snd_soc_put_enum_double - enumerated double mixer put callback
  2055. * @kcontrol: mixer control
  2056. * @ucontrol: control element information
  2057. *
  2058. * Callback to set the value of a double enumerated mixer.
  2059. *
  2060. * Returns 0 for success.
  2061. */
  2062. int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
  2063. struct snd_ctl_elem_value *ucontrol)
  2064. {
  2065. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2066. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  2067. unsigned int val;
  2068. unsigned int mask;
  2069. if (ucontrol->value.enumerated.item[0] > e->max - 1)
  2070. return -EINVAL;
  2071. val = ucontrol->value.enumerated.item[0] << e->shift_l;
  2072. mask = e->mask << e->shift_l;
  2073. if (e->shift_l != e->shift_r) {
  2074. if (ucontrol->value.enumerated.item[1] > e->max - 1)
  2075. return -EINVAL;
  2076. val |= ucontrol->value.enumerated.item[1] << e->shift_r;
  2077. mask |= e->mask << e->shift_r;
  2078. }
  2079. return snd_soc_update_bits_locked(codec, e->reg, mask, val);
  2080. }
  2081. EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
  2082. /**
  2083. * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
  2084. * @kcontrol: mixer control
  2085. * @ucontrol: control element information
  2086. *
  2087. * Callback to get the value of a double semi enumerated mixer.
  2088. *
  2089. * Semi enumerated mixer: the enumerated items are referred as values. Can be
  2090. * used for handling bitfield coded enumeration for example.
  2091. *
  2092. * Returns 0 for success.
  2093. */
  2094. int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
  2095. struct snd_ctl_elem_value *ucontrol)
  2096. {
  2097. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2098. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  2099. unsigned int reg_val, val, mux;
  2100. reg_val = snd_soc_read(codec, e->reg);
  2101. val = (reg_val >> e->shift_l) & e->mask;
  2102. for (mux = 0; mux < e->max; mux++) {
  2103. if (val == e->values[mux])
  2104. break;
  2105. }
  2106. ucontrol->value.enumerated.item[0] = mux;
  2107. if (e->shift_l != e->shift_r) {
  2108. val = (reg_val >> e->shift_r) & e->mask;
  2109. for (mux = 0; mux < e->max; mux++) {
  2110. if (val == e->values[mux])
  2111. break;
  2112. }
  2113. ucontrol->value.enumerated.item[1] = mux;
  2114. }
  2115. return 0;
  2116. }
  2117. EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
  2118. /**
  2119. * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
  2120. * @kcontrol: mixer control
  2121. * @ucontrol: control element information
  2122. *
  2123. * Callback to set the value of a double semi enumerated mixer.
  2124. *
  2125. * Semi enumerated mixer: the enumerated items are referred as values. Can be
  2126. * used for handling bitfield coded enumeration for example.
  2127. *
  2128. * Returns 0 for success.
  2129. */
  2130. int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
  2131. struct snd_ctl_elem_value *ucontrol)
  2132. {
  2133. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2134. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  2135. unsigned int val;
  2136. unsigned int mask;
  2137. if (ucontrol->value.enumerated.item[0] > e->max - 1)
  2138. return -EINVAL;
  2139. val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
  2140. mask = e->mask << e->shift_l;
  2141. if (e->shift_l != e->shift_r) {
  2142. if (ucontrol->value.enumerated.item[1] > e->max - 1)
  2143. return -EINVAL;
  2144. val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
  2145. mask |= e->mask << e->shift_r;
  2146. }
  2147. return snd_soc_update_bits_locked(codec, e->reg, mask, val);
  2148. }
  2149. EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
  2150. /**
  2151. * snd_soc_info_enum_ext - external enumerated single mixer info callback
  2152. * @kcontrol: mixer control
  2153. * @uinfo: control element information
  2154. *
  2155. * Callback to provide information about an external enumerated
  2156. * single mixer.
  2157. *
  2158. * Returns 0 for success.
  2159. */
  2160. int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
  2161. struct snd_ctl_elem_info *uinfo)
  2162. {
  2163. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  2164. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  2165. uinfo->count = 1;
  2166. uinfo->value.enumerated.items = e->max;
  2167. if (uinfo->value.enumerated.item > e->max - 1)
  2168. uinfo->value.enumerated.item = e->max - 1;
  2169. strcpy(uinfo->value.enumerated.name,
  2170. e->texts[uinfo->value.enumerated.item]);
  2171. return 0;
  2172. }
  2173. EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
  2174. /**
  2175. * snd_soc_info_volsw - single mixer info callback
  2176. * @kcontrol: mixer control
  2177. * @uinfo: control element information
  2178. *
  2179. * Callback to provide information about a single mixer control, or a double
  2180. * mixer control that spans 2 registers.
  2181. *
  2182. * Returns 0 for success.
  2183. */
  2184. int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
  2185. struct snd_ctl_elem_info *uinfo)
  2186. {
  2187. struct soc_mixer_control *mc =
  2188. (struct soc_mixer_control *)kcontrol->private_value;
  2189. int platform_max;
  2190. if (!mc->platform_max)
  2191. mc->platform_max = mc->max;
  2192. platform_max = mc->platform_max;
  2193. if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
  2194. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  2195. else
  2196. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2197. uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
  2198. uinfo->value.integer.min = 0;
  2199. uinfo->value.integer.max = platform_max;
  2200. return 0;
  2201. }
  2202. EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
  2203. /**
  2204. * snd_soc_get_volsw - single mixer get callback
  2205. * @kcontrol: mixer control
  2206. * @ucontrol: control element information
  2207. *
  2208. * Callback to get the value of a single mixer control, or a double mixer
  2209. * control that spans 2 registers.
  2210. *
  2211. * Returns 0 for success.
  2212. */
  2213. int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
  2214. struct snd_ctl_elem_value *ucontrol)
  2215. {
  2216. struct soc_mixer_control *mc =
  2217. (struct soc_mixer_control *)kcontrol->private_value;
  2218. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2219. unsigned int reg = mc->reg;
  2220. unsigned int reg2 = mc->rreg;
  2221. unsigned int shift = mc->shift;
  2222. unsigned int rshift = mc->rshift;
  2223. int max = mc->max;
  2224. unsigned int mask = (1 << fls(max)) - 1;
  2225. unsigned int invert = mc->invert;
  2226. ucontrol->value.integer.value[0] =
  2227. (snd_soc_read(codec, reg) >> shift) & mask;
  2228. if (invert)
  2229. ucontrol->value.integer.value[0] =
  2230. max - ucontrol->value.integer.value[0];
  2231. if (snd_soc_volsw_is_stereo(mc)) {
  2232. if (reg == reg2)
  2233. ucontrol->value.integer.value[1] =
  2234. (snd_soc_read(codec, reg) >> rshift) & mask;
  2235. else
  2236. ucontrol->value.integer.value[1] =
  2237. (snd_soc_read(codec, reg2) >> shift) & mask;
  2238. if (invert)
  2239. ucontrol->value.integer.value[1] =
  2240. max - ucontrol->value.integer.value[1];
  2241. }
  2242. return 0;
  2243. }
  2244. EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
  2245. /**
  2246. * snd_soc_put_volsw - single mixer put callback
  2247. * @kcontrol: mixer control
  2248. * @ucontrol: control element information
  2249. *
  2250. * Callback to set the value of a single mixer control, or a double mixer
  2251. * control that spans 2 registers.
  2252. *
  2253. * Returns 0 for success.
  2254. */
  2255. int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
  2256. struct snd_ctl_elem_value *ucontrol)
  2257. {
  2258. struct soc_mixer_control *mc =
  2259. (struct soc_mixer_control *)kcontrol->private_value;
  2260. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2261. unsigned int reg = mc->reg;
  2262. unsigned int reg2 = mc->rreg;
  2263. unsigned int shift = mc->shift;
  2264. unsigned int rshift = mc->rshift;
  2265. int max = mc->max;
  2266. unsigned int mask = (1 << fls(max)) - 1;
  2267. unsigned int invert = mc->invert;
  2268. int err;
  2269. bool type_2r = 0;
  2270. unsigned int val2 = 0;
  2271. unsigned int val, val_mask;
  2272. val = (ucontrol->value.integer.value[0] & mask);
  2273. if (invert)
  2274. val = max - val;
  2275. val_mask = mask << shift;
  2276. val = val << shift;
  2277. if (snd_soc_volsw_is_stereo(mc)) {
  2278. val2 = (ucontrol->value.integer.value[1] & mask);
  2279. if (invert)
  2280. val2 = max - val2;
  2281. if (reg == reg2) {
  2282. val_mask |= mask << rshift;
  2283. val |= val2 << rshift;
  2284. } else {
  2285. val2 = val2 << shift;
  2286. type_2r = 1;
  2287. }
  2288. }
  2289. err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
  2290. if (err < 0)
  2291. return err;
  2292. if (type_2r)
  2293. err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
  2294. return err;
  2295. }
  2296. EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
  2297. /**
  2298. * snd_soc_get_volsw_sx - single mixer get callback
  2299. * @kcontrol: mixer control
  2300. * @ucontrol: control element information
  2301. *
  2302. * Callback to get the value of a single mixer control, or a double mixer
  2303. * control that spans 2 registers.
  2304. *
  2305. * Returns 0 for success.
  2306. */
  2307. int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
  2308. struct snd_ctl_elem_value *ucontrol)
  2309. {
  2310. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2311. struct soc_mixer_control *mc =
  2312. (struct soc_mixer_control *)kcontrol->private_value;
  2313. unsigned int reg = mc->reg;
  2314. unsigned int reg2 = mc->rreg;
  2315. unsigned int shift = mc->shift;
  2316. unsigned int rshift = mc->rshift;
  2317. int max = mc->max;
  2318. int min = mc->min;
  2319. int mask = (1 << (fls(min + max) - 1)) - 1;
  2320. ucontrol->value.integer.value[0] =
  2321. ((snd_soc_read(codec, reg) >> shift) - min) & mask;
  2322. if (snd_soc_volsw_is_stereo(mc))
  2323. ucontrol->value.integer.value[1] =
  2324. ((snd_soc_read(codec, reg2) >> rshift) - min) & mask;
  2325. return 0;
  2326. }
  2327. EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
  2328. /**
  2329. * snd_soc_put_volsw_sx - double mixer set callback
  2330. * @kcontrol: mixer control
  2331. * @uinfo: control element information
  2332. *
  2333. * Callback to set the value of a double mixer control that spans 2 registers.
  2334. *
  2335. * Returns 0 for success.
  2336. */
  2337. int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
  2338. struct snd_ctl_elem_value *ucontrol)
  2339. {
  2340. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2341. struct soc_mixer_control *mc =
  2342. (struct soc_mixer_control *)kcontrol->private_value;
  2343. unsigned int reg = mc->reg;
  2344. unsigned int reg2 = mc->rreg;
  2345. unsigned int shift = mc->shift;
  2346. unsigned int rshift = mc->rshift;
  2347. int max = mc->max;
  2348. int min = mc->min;
  2349. int mask = (1 << (fls(min + max) - 1)) - 1;
  2350. int err = 0;
  2351. unsigned short val, val_mask, val2 = 0;
  2352. val_mask = mask << shift;
  2353. val = (ucontrol->value.integer.value[0] + min) & mask;
  2354. val = val << shift;
  2355. err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
  2356. if (err < 0)
  2357. return err;
  2358. if (snd_soc_volsw_is_stereo(mc)) {
  2359. val_mask = mask << rshift;
  2360. val2 = (ucontrol->value.integer.value[1] + min) & mask;
  2361. val2 = val2 << rshift;
  2362. if (snd_soc_update_bits_locked(codec, reg2, val_mask, val2))
  2363. return err;
  2364. }
  2365. return 0;
  2366. }
  2367. EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
  2368. /**
  2369. * snd_soc_info_volsw_s8 - signed mixer info callback
  2370. * @kcontrol: mixer control
  2371. * @uinfo: control element information
  2372. *
  2373. * Callback to provide information about a signed mixer control.
  2374. *
  2375. * Returns 0 for success.
  2376. */
  2377. int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
  2378. struct snd_ctl_elem_info *uinfo)
  2379. {
  2380. struct soc_mixer_control *mc =
  2381. (struct soc_mixer_control *)kcontrol->private_value;
  2382. int platform_max;
  2383. int min = mc->min;
  2384. if (!mc->platform_max)
  2385. mc->platform_max = mc->max;
  2386. platform_max = mc->platform_max;
  2387. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2388. uinfo->count = 2;
  2389. uinfo->value.integer.min = 0;
  2390. uinfo->value.integer.max = platform_max - min;
  2391. return 0;
  2392. }
  2393. EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
  2394. /**
  2395. * snd_soc_get_volsw_s8 - signed mixer get callback
  2396. * @kcontrol: mixer control
  2397. * @ucontrol: control element information
  2398. *
  2399. * Callback to get the value of a signed mixer control.
  2400. *
  2401. * Returns 0 for success.
  2402. */
  2403. int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
  2404. struct snd_ctl_elem_value *ucontrol)
  2405. {
  2406. struct soc_mixer_control *mc =
  2407. (struct soc_mixer_control *)kcontrol->private_value;
  2408. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2409. unsigned int reg = mc->reg;
  2410. int min = mc->min;
  2411. int val = snd_soc_read(codec, reg);
  2412. ucontrol->value.integer.value[0] =
  2413. ((signed char)(val & 0xff))-min;
  2414. ucontrol->value.integer.value[1] =
  2415. ((signed char)((val >> 8) & 0xff))-min;
  2416. return 0;
  2417. }
  2418. EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
  2419. /**
  2420. * snd_soc_put_volsw_sgn - signed mixer put callback
  2421. * @kcontrol: mixer control
  2422. * @ucontrol: control element information
  2423. *
  2424. * Callback to set the value of a signed mixer control.
  2425. *
  2426. * Returns 0 for success.
  2427. */
  2428. int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
  2429. struct snd_ctl_elem_value *ucontrol)
  2430. {
  2431. struct soc_mixer_control *mc =
  2432. (struct soc_mixer_control *)kcontrol->private_value;
  2433. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2434. unsigned int reg = mc->reg;
  2435. int min = mc->min;
  2436. unsigned int val;
  2437. val = (ucontrol->value.integer.value[0]+min) & 0xff;
  2438. val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
  2439. return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
  2440. }
  2441. EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
  2442. /**
  2443. * snd_soc_info_volsw_range - single mixer info callback with range.
  2444. * @kcontrol: mixer control
  2445. * @uinfo: control element information
  2446. *
  2447. * Callback to provide information, within a range, about a single
  2448. * mixer control.
  2449. *
  2450. * returns 0 for success.
  2451. */
  2452. int snd_soc_info_volsw_range(struct snd_kcontrol *kcontrol,
  2453. struct snd_ctl_elem_info *uinfo)
  2454. {
  2455. struct soc_mixer_control *mc =
  2456. (struct soc_mixer_control *)kcontrol->private_value;
  2457. int platform_max;
  2458. int min = mc->min;
  2459. if (!mc->platform_max)
  2460. mc->platform_max = mc->max;
  2461. platform_max = mc->platform_max;
  2462. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2463. uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
  2464. uinfo->value.integer.min = 0;
  2465. uinfo->value.integer.max = platform_max - min;
  2466. return 0;
  2467. }
  2468. EXPORT_SYMBOL_GPL(snd_soc_info_volsw_range);
  2469. /**
  2470. * snd_soc_put_volsw_range - single mixer put value callback with range.
  2471. * @kcontrol: mixer control
  2472. * @ucontrol: control element information
  2473. *
  2474. * Callback to set the value, within a range, for a single mixer control.
  2475. *
  2476. * Returns 0 for success.
  2477. */
  2478. int snd_soc_put_volsw_range(struct snd_kcontrol *kcontrol,
  2479. struct snd_ctl_elem_value *ucontrol)
  2480. {
  2481. struct soc_mixer_control *mc =
  2482. (struct soc_mixer_control *)kcontrol->private_value;
  2483. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2484. unsigned int reg = mc->reg;
  2485. unsigned int rreg = mc->rreg;
  2486. unsigned int shift = mc->shift;
  2487. int min = mc->min;
  2488. int max = mc->max;
  2489. unsigned int mask = (1 << fls(max)) - 1;
  2490. unsigned int invert = mc->invert;
  2491. unsigned int val, val_mask;
  2492. int ret;
  2493. val = ((ucontrol->value.integer.value[0] + min) & mask);
  2494. if (invert)
  2495. val = max - val;
  2496. val_mask = mask << shift;
  2497. val = val << shift;
  2498. ret = snd_soc_update_bits_locked(codec, reg, val_mask, val);
  2499. if (ret < 0)
  2500. return ret;
  2501. if (snd_soc_volsw_is_stereo(mc)) {
  2502. val = ((ucontrol->value.integer.value[1] + min) & mask);
  2503. if (invert)
  2504. val = max - val;
  2505. val_mask = mask << shift;
  2506. val = val << shift;
  2507. ret = snd_soc_update_bits_locked(codec, rreg, val_mask, val);
  2508. }
  2509. return ret;
  2510. }
  2511. EXPORT_SYMBOL_GPL(snd_soc_put_volsw_range);
  2512. /**
  2513. * snd_soc_get_volsw_range - single mixer get callback with range
  2514. * @kcontrol: mixer control
  2515. * @ucontrol: control element information
  2516. *
  2517. * Callback to get the value, within a range, of a single mixer control.
  2518. *
  2519. * Returns 0 for success.
  2520. */
  2521. int snd_soc_get_volsw_range(struct snd_kcontrol *kcontrol,
  2522. struct snd_ctl_elem_value *ucontrol)
  2523. {
  2524. struct soc_mixer_control *mc =
  2525. (struct soc_mixer_control *)kcontrol->private_value;
  2526. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2527. unsigned int reg = mc->reg;
  2528. unsigned int rreg = mc->rreg;
  2529. unsigned int shift = mc->shift;
  2530. int min = mc->min;
  2531. int max = mc->max;
  2532. unsigned int mask = (1 << fls(max)) - 1;
  2533. unsigned int invert = mc->invert;
  2534. ucontrol->value.integer.value[0] =
  2535. (snd_soc_read(codec, reg) >> shift) & mask;
  2536. if (invert)
  2537. ucontrol->value.integer.value[0] =
  2538. max - ucontrol->value.integer.value[0];
  2539. ucontrol->value.integer.value[0] =
  2540. ucontrol->value.integer.value[0] - min;
  2541. if (snd_soc_volsw_is_stereo(mc)) {
  2542. ucontrol->value.integer.value[1] =
  2543. (snd_soc_read(codec, rreg) >> shift) & mask;
  2544. if (invert)
  2545. ucontrol->value.integer.value[1] =
  2546. max - ucontrol->value.integer.value[1];
  2547. ucontrol->value.integer.value[1] =
  2548. ucontrol->value.integer.value[1] - min;
  2549. }
  2550. return 0;
  2551. }
  2552. EXPORT_SYMBOL_GPL(snd_soc_get_volsw_range);
  2553. /**
  2554. * snd_soc_limit_volume - Set new limit to an existing volume control.
  2555. *
  2556. * @codec: where to look for the control
  2557. * @name: Name of the control
  2558. * @max: new maximum limit
  2559. *
  2560. * Return 0 for success, else error.
  2561. */
  2562. int snd_soc_limit_volume(struct snd_soc_codec *codec,
  2563. const char *name, int max)
  2564. {
  2565. struct snd_card *card = codec->card->snd_card;
  2566. struct snd_kcontrol *kctl;
  2567. struct soc_mixer_control *mc;
  2568. int found = 0;
  2569. int ret = -EINVAL;
  2570. /* Sanity check for name and max */
  2571. if (unlikely(!name || max <= 0))
  2572. return -EINVAL;
  2573. list_for_each_entry(kctl, &card->controls, list) {
  2574. if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
  2575. found = 1;
  2576. break;
  2577. }
  2578. }
  2579. if (found) {
  2580. mc = (struct soc_mixer_control *)kctl->private_value;
  2581. if (max <= mc->max) {
  2582. mc->platform_max = max;
  2583. ret = 0;
  2584. }
  2585. }
  2586. return ret;
  2587. }
  2588. EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
  2589. int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
  2590. struct snd_ctl_elem_info *uinfo)
  2591. {
  2592. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2593. struct soc_bytes *params = (void *)kcontrol->private_value;
  2594. uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
  2595. uinfo->count = params->num_regs * codec->val_bytes;
  2596. return 0;
  2597. }
  2598. EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
  2599. int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
  2600. struct snd_ctl_elem_value *ucontrol)
  2601. {
  2602. struct soc_bytes *params = (void *)kcontrol->private_value;
  2603. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2604. int ret;
  2605. if (codec->using_regmap)
  2606. ret = regmap_raw_read(codec->control_data, params->base,
  2607. ucontrol->value.bytes.data,
  2608. params->num_regs * codec->val_bytes);
  2609. else
  2610. ret = -EINVAL;
  2611. /* Hide any masked bytes to ensure consistent data reporting */
  2612. if (ret == 0 && params->mask) {
  2613. switch (codec->val_bytes) {
  2614. case 1:
  2615. ucontrol->value.bytes.data[0] &= ~params->mask;
  2616. break;
  2617. case 2:
  2618. ((u16 *)(&ucontrol->value.bytes.data))[0]
  2619. &= ~params->mask;
  2620. break;
  2621. case 4:
  2622. ((u32 *)(&ucontrol->value.bytes.data))[0]
  2623. &= ~params->mask;
  2624. break;
  2625. default:
  2626. return -EINVAL;
  2627. }
  2628. }
  2629. return ret;
  2630. }
  2631. EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
  2632. int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
  2633. struct snd_ctl_elem_value *ucontrol)
  2634. {
  2635. struct soc_bytes *params = (void *)kcontrol->private_value;
  2636. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2637. int ret, len;
  2638. unsigned int val;
  2639. void *data;
  2640. if (!codec->using_regmap)
  2641. return -EINVAL;
  2642. len = params->num_regs * codec->val_bytes;
  2643. data = kmemdup(ucontrol->value.bytes.data, len, GFP_KERNEL | GFP_DMA);
  2644. if (!data)
  2645. return -ENOMEM;
  2646. /*
  2647. * If we've got a mask then we need to preserve the register
  2648. * bits. We shouldn't modify the incoming data so take a
  2649. * copy.
  2650. */
  2651. if (params->mask) {
  2652. ret = regmap_read(codec->control_data, params->base, &val);
  2653. if (ret != 0)
  2654. goto out;
  2655. val &= params->mask;
  2656. switch (codec->val_bytes) {
  2657. case 1:
  2658. ((u8 *)data)[0] &= ~params->mask;
  2659. ((u8 *)data)[0] |= val;
  2660. break;
  2661. case 2:
  2662. ((u16 *)data)[0] &= cpu_to_be16(~params->mask);
  2663. ((u16 *)data)[0] |= cpu_to_be16(val);
  2664. break;
  2665. case 4:
  2666. ((u32 *)data)[0] &= cpu_to_be32(~params->mask);
  2667. ((u32 *)data)[0] |= cpu_to_be32(val);
  2668. break;
  2669. default:
  2670. ret = -EINVAL;
  2671. goto out;
  2672. }
  2673. }
  2674. ret = regmap_raw_write(codec->control_data, params->base,
  2675. data, len);
  2676. out:
  2677. kfree(data);
  2678. return ret;
  2679. }
  2680. EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
  2681. /**
  2682. * snd_soc_info_xr_sx - signed multi register info callback
  2683. * @kcontrol: mreg control
  2684. * @uinfo: control element information
  2685. *
  2686. * Callback to provide information of a control that can
  2687. * span multiple codec registers which together
  2688. * forms a single signed value in a MSB/LSB manner.
  2689. *
  2690. * Returns 0 for success.
  2691. */
  2692. int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol,
  2693. struct snd_ctl_elem_info *uinfo)
  2694. {
  2695. struct soc_mreg_control *mc =
  2696. (struct soc_mreg_control *)kcontrol->private_value;
  2697. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2698. uinfo->count = 1;
  2699. uinfo->value.integer.min = mc->min;
  2700. uinfo->value.integer.max = mc->max;
  2701. return 0;
  2702. }
  2703. EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx);
  2704. /**
  2705. * snd_soc_get_xr_sx - signed multi register get callback
  2706. * @kcontrol: mreg control
  2707. * @ucontrol: control element information
  2708. *
  2709. * Callback to get the value of a control that can span
  2710. * multiple codec registers which together forms a single
  2711. * signed value in a MSB/LSB manner. The control supports
  2712. * specifying total no of bits used to allow for bitfields
  2713. * across the multiple codec registers.
  2714. *
  2715. * Returns 0 for success.
  2716. */
  2717. int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol,
  2718. struct snd_ctl_elem_value *ucontrol)
  2719. {
  2720. struct soc_mreg_control *mc =
  2721. (struct soc_mreg_control *)kcontrol->private_value;
  2722. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2723. unsigned int regbase = mc->regbase;
  2724. unsigned int regcount = mc->regcount;
  2725. unsigned int regwshift = codec->driver->reg_word_size * BITS_PER_BYTE;
  2726. unsigned int regwmask = (1<<regwshift)-1;
  2727. unsigned int invert = mc->invert;
  2728. unsigned long mask = (1UL<<mc->nbits)-1;
  2729. long min = mc->min;
  2730. long max = mc->max;
  2731. long val = 0;
  2732. unsigned long regval;
  2733. unsigned int i;
  2734. for (i = 0; i < regcount; i++) {
  2735. regval = snd_soc_read(codec, regbase+i) & regwmask;
  2736. val |= regval << (regwshift*(regcount-i-1));
  2737. }
  2738. val &= mask;
  2739. if (min < 0 && val > max)
  2740. val |= ~mask;
  2741. if (invert)
  2742. val = max - val;
  2743. ucontrol->value.integer.value[0] = val;
  2744. return 0;
  2745. }
  2746. EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx);
  2747. /**
  2748. * snd_soc_put_xr_sx - signed multi register get callback
  2749. * @kcontrol: mreg control
  2750. * @ucontrol: control element information
  2751. *
  2752. * Callback to set the value of a control that can span
  2753. * multiple codec registers which together forms a single
  2754. * signed value in a MSB/LSB manner. The control supports
  2755. * specifying total no of bits used to allow for bitfields
  2756. * across the multiple codec registers.
  2757. *
  2758. * Returns 0 for success.
  2759. */
  2760. int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol,
  2761. struct snd_ctl_elem_value *ucontrol)
  2762. {
  2763. struct soc_mreg_control *mc =
  2764. (struct soc_mreg_control *)kcontrol->private_value;
  2765. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2766. unsigned int regbase = mc->regbase;
  2767. unsigned int regcount = mc->regcount;
  2768. unsigned int regwshift = codec->driver->reg_word_size * BITS_PER_BYTE;
  2769. unsigned int regwmask = (1<<regwshift)-1;
  2770. unsigned int invert = mc->invert;
  2771. unsigned long mask = (1UL<<mc->nbits)-1;
  2772. long max = mc->max;
  2773. long val = ucontrol->value.integer.value[0];
  2774. unsigned int i, regval, regmask;
  2775. int err;
  2776. if (invert)
  2777. val = max - val;
  2778. val &= mask;
  2779. for (i = 0; i < regcount; i++) {
  2780. regval = (val >> (regwshift*(regcount-i-1))) & regwmask;
  2781. regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask;
  2782. err = snd_soc_update_bits_locked(codec, regbase+i,
  2783. regmask, regval);
  2784. if (err < 0)
  2785. return err;
  2786. }
  2787. return 0;
  2788. }
  2789. EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx);
  2790. /**
  2791. * snd_soc_get_strobe - strobe get callback
  2792. * @kcontrol: mixer control
  2793. * @ucontrol: control element information
  2794. *
  2795. * Callback get the value of a strobe mixer control.
  2796. *
  2797. * Returns 0 for success.
  2798. */
  2799. int snd_soc_get_strobe(struct snd_kcontrol *kcontrol,
  2800. struct snd_ctl_elem_value *ucontrol)
  2801. {
  2802. struct soc_mixer_control *mc =
  2803. (struct soc_mixer_control *)kcontrol->private_value;
  2804. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2805. unsigned int reg = mc->reg;
  2806. unsigned int shift = mc->shift;
  2807. unsigned int mask = 1 << shift;
  2808. unsigned int invert = mc->invert != 0;
  2809. unsigned int val = snd_soc_read(codec, reg) & mask;
  2810. if (shift != 0 && val != 0)
  2811. val = val >> shift;
  2812. ucontrol->value.enumerated.item[0] = val ^ invert;
  2813. return 0;
  2814. }
  2815. EXPORT_SYMBOL_GPL(snd_soc_get_strobe);
  2816. /**
  2817. * snd_soc_put_strobe - strobe put callback
  2818. * @kcontrol: mixer control
  2819. * @ucontrol: control element information
  2820. *
  2821. * Callback strobe a register bit to high then low (or the inverse)
  2822. * in one pass of a single mixer enum control.
  2823. *
  2824. * Returns 1 for success.
  2825. */
  2826. int snd_soc_put_strobe(struct snd_kcontrol *kcontrol,
  2827. struct snd_ctl_elem_value *ucontrol)
  2828. {
  2829. struct soc_mixer_control *mc =
  2830. (struct soc_mixer_control *)kcontrol->private_value;
  2831. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2832. unsigned int reg = mc->reg;
  2833. unsigned int shift = mc->shift;
  2834. unsigned int mask = 1 << shift;
  2835. unsigned int invert = mc->invert != 0;
  2836. unsigned int strobe = ucontrol->value.enumerated.item[0] != 0;
  2837. unsigned int val1 = (strobe ^ invert) ? mask : 0;
  2838. unsigned int val2 = (strobe ^ invert) ? 0 : mask;
  2839. int err;
  2840. err = snd_soc_update_bits_locked(codec, reg, mask, val1);
  2841. if (err < 0)
  2842. return err;
  2843. err = snd_soc_update_bits_locked(codec, reg, mask, val2);
  2844. return err;
  2845. }
  2846. EXPORT_SYMBOL_GPL(snd_soc_put_strobe);
  2847. /**
  2848. * snd_soc_dai_set_sysclk - configure DAI system or master clock.
  2849. * @dai: DAI
  2850. * @clk_id: DAI specific clock ID
  2851. * @freq: new clock frequency in Hz
  2852. * @dir: new clock direction - input/output.
  2853. *
  2854. * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
  2855. */
  2856. int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
  2857. unsigned int freq, int dir)
  2858. {
  2859. if (dai->driver && dai->driver->ops->set_sysclk)
  2860. return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
  2861. else if (dai->codec && dai->codec->driver->set_sysclk)
  2862. return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
  2863. freq, dir);
  2864. else
  2865. return -EINVAL;
  2866. }
  2867. EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
  2868. /**
  2869. * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
  2870. * @codec: CODEC
  2871. * @clk_id: DAI specific clock ID
  2872. * @source: Source for the clock
  2873. * @freq: new clock frequency in Hz
  2874. * @dir: new clock direction - input/output.
  2875. *
  2876. * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
  2877. */
  2878. int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
  2879. int source, unsigned int freq, int dir)
  2880. {
  2881. if (codec->driver->set_sysclk)
  2882. return codec->driver->set_sysclk(codec, clk_id, source,
  2883. freq, dir);
  2884. else
  2885. return -EINVAL;
  2886. }
  2887. EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
  2888. /**
  2889. * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
  2890. * @dai: DAI
  2891. * @div_id: DAI specific clock divider ID
  2892. * @div: new clock divisor.
  2893. *
  2894. * Configures the clock dividers. This is used to derive the best DAI bit and
  2895. * frame clocks from the system or master clock. It's best to set the DAI bit
  2896. * and frame clocks as low as possible to save system power.
  2897. */
  2898. int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
  2899. int div_id, int div)
  2900. {
  2901. if (dai->driver && dai->driver->ops->set_clkdiv)
  2902. return dai->driver->ops->set_clkdiv(dai, div_id, div);
  2903. else
  2904. return -EINVAL;
  2905. }
  2906. EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
  2907. /**
  2908. * snd_soc_dai_set_pll - configure DAI PLL.
  2909. * @dai: DAI
  2910. * @pll_id: DAI specific PLL ID
  2911. * @source: DAI specific source for the PLL
  2912. * @freq_in: PLL input clock frequency in Hz
  2913. * @freq_out: requested PLL output clock frequency in Hz
  2914. *
  2915. * Configures and enables PLL to generate output clock based on input clock.
  2916. */
  2917. int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
  2918. unsigned int freq_in, unsigned int freq_out)
  2919. {
  2920. if (dai->driver && dai->driver->ops->set_pll)
  2921. return dai->driver->ops->set_pll(dai, pll_id, source,
  2922. freq_in, freq_out);
  2923. else if (dai->codec && dai->codec->driver->set_pll)
  2924. return dai->codec->driver->set_pll(dai->codec, pll_id, source,
  2925. freq_in, freq_out);
  2926. else
  2927. return -EINVAL;
  2928. }
  2929. EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
  2930. /*
  2931. * snd_soc_codec_set_pll - configure codec PLL.
  2932. * @codec: CODEC
  2933. * @pll_id: DAI specific PLL ID
  2934. * @source: DAI specific source for the PLL
  2935. * @freq_in: PLL input clock frequency in Hz
  2936. * @freq_out: requested PLL output clock frequency in Hz
  2937. *
  2938. * Configures and enables PLL to generate output clock based on input clock.
  2939. */
  2940. int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
  2941. unsigned int freq_in, unsigned int freq_out)
  2942. {
  2943. if (codec->driver->set_pll)
  2944. return codec->driver->set_pll(codec, pll_id, source,
  2945. freq_in, freq_out);
  2946. else
  2947. return -EINVAL;
  2948. }
  2949. EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
  2950. /**
  2951. * snd_soc_dai_set_fmt - configure DAI hardware audio format.
  2952. * @dai: DAI
  2953. * @fmt: SND_SOC_DAIFMT_ format value.
  2954. *
  2955. * Configures the DAI hardware format and clocking.
  2956. */
  2957. int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  2958. {
  2959. if (dai->driver == NULL)
  2960. return -EINVAL;
  2961. if (dai->driver->ops->set_fmt == NULL)
  2962. return -ENOTSUPP;
  2963. return dai->driver->ops->set_fmt(dai, fmt);
  2964. }
  2965. EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
  2966. /**
  2967. * snd_soc_dai_set_tdm_slot - configure DAI TDM.
  2968. * @dai: DAI
  2969. * @tx_mask: bitmask representing active TX slots.
  2970. * @rx_mask: bitmask representing active RX slots.
  2971. * @slots: Number of slots in use.
  2972. * @slot_width: Width in bits for each slot.
  2973. *
  2974. * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
  2975. * specific.
  2976. */
  2977. int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
  2978. unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
  2979. {
  2980. if (dai->driver && dai->driver->ops->set_tdm_slot)
  2981. return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
  2982. slots, slot_width);
  2983. else
  2984. return -EINVAL;
  2985. }
  2986. EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
  2987. /**
  2988. * snd_soc_dai_set_channel_map - configure DAI audio channel map
  2989. * @dai: DAI
  2990. * @tx_num: how many TX channels
  2991. * @tx_slot: pointer to an array which imply the TX slot number channel
  2992. * 0~num-1 uses
  2993. * @rx_num: how many RX channels
  2994. * @rx_slot: pointer to an array which imply the RX slot number channel
  2995. * 0~num-1 uses
  2996. *
  2997. * configure the relationship between channel number and TDM slot number.
  2998. */
  2999. int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
  3000. unsigned int tx_num, unsigned int *tx_slot,
  3001. unsigned int rx_num, unsigned int *rx_slot)
  3002. {
  3003. if (dai->driver && dai->driver->ops->set_channel_map)
  3004. return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
  3005. rx_num, rx_slot);
  3006. else
  3007. return -EINVAL;
  3008. }
  3009. EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
  3010. /**
  3011. * snd_soc_dai_set_tristate - configure DAI system or master clock.
  3012. * @dai: DAI
  3013. * @tristate: tristate enable
  3014. *
  3015. * Tristates the DAI so that others can use it.
  3016. */
  3017. int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
  3018. {
  3019. if (dai->driver && dai->driver->ops->set_tristate)
  3020. return dai->driver->ops->set_tristate(dai, tristate);
  3021. else
  3022. return -EINVAL;
  3023. }
  3024. EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
  3025. /**
  3026. * snd_soc_dai_digital_mute - configure DAI system or master clock.
  3027. * @dai: DAI
  3028. * @mute: mute enable
  3029. * @direction: stream to mute
  3030. *
  3031. * Mutes the DAI DAC.
  3032. */
  3033. int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
  3034. int direction)
  3035. {
  3036. if (!dai->driver)
  3037. return -ENOTSUPP;
  3038. if (dai->driver->ops->mute_stream)
  3039. return dai->driver->ops->mute_stream(dai, mute, direction);
  3040. else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
  3041. dai->driver->ops->digital_mute)
  3042. return dai->driver->ops->digital_mute(dai, mute);
  3043. else
  3044. return -ENOTSUPP;
  3045. }
  3046. EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
  3047. /**
  3048. * snd_soc_register_card - Register a card with the ASoC core
  3049. *
  3050. * @card: Card to register
  3051. *
  3052. */
  3053. int snd_soc_register_card(struct snd_soc_card *card)
  3054. {
  3055. int i, ret;
  3056. if (!card->name || !card->dev)
  3057. return -EINVAL;
  3058. for (i = 0; i < card->num_links; i++) {
  3059. struct snd_soc_dai_link *link = &card->dai_link[i];
  3060. /*
  3061. * Codec must be specified by 1 of name or OF node,
  3062. * not both or neither.
  3063. */
  3064. if (!!link->codec_name == !!link->codec_of_node) {
  3065. dev_err(card->dev,
  3066. "ASoC: Neither/both codec name/of_node are set for %s\n",
  3067. link->name);
  3068. return -EINVAL;
  3069. }
  3070. /* Codec DAI name must be specified */
  3071. if (!link->codec_dai_name) {
  3072. dev_err(card->dev,
  3073. "ASoC: codec_dai_name not set for %s\n",
  3074. link->name);
  3075. return -EINVAL;
  3076. }
  3077. /*
  3078. * Platform may be specified by either name or OF node, but
  3079. * can be left unspecified, and a dummy platform will be used.
  3080. */
  3081. if (link->platform_name && link->platform_of_node) {
  3082. dev_err(card->dev,
  3083. "ASoC: Both platform name/of_node are set for %s\n",
  3084. link->name);
  3085. return -EINVAL;
  3086. }
  3087. /*
  3088. * CPU device may be specified by either name or OF node, but
  3089. * can be left unspecified, and will be matched based on DAI
  3090. * name alone..
  3091. */
  3092. if (link->cpu_name && link->cpu_of_node) {
  3093. dev_err(card->dev,
  3094. "ASoC: Neither/both cpu name/of_node are set for %s\n",
  3095. link->name);
  3096. return -EINVAL;
  3097. }
  3098. /*
  3099. * At least one of CPU DAI name or CPU device name/node must be
  3100. * specified
  3101. */
  3102. if (!link->cpu_dai_name &&
  3103. !(link->cpu_name || link->cpu_of_node)) {
  3104. dev_err(card->dev,
  3105. "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
  3106. link->name);
  3107. return -EINVAL;
  3108. }
  3109. }
  3110. dev_set_drvdata(card->dev, card);
  3111. snd_soc_initialize_card_lists(card);
  3112. soc_init_card_debugfs(card);
  3113. card->rtd = devm_kzalloc(card->dev,
  3114. sizeof(struct snd_soc_pcm_runtime) *
  3115. (card->num_links + card->num_aux_devs),
  3116. GFP_KERNEL);
  3117. if (card->rtd == NULL)
  3118. return -ENOMEM;
  3119. card->num_rtd = 0;
  3120. card->rtd_aux = &card->rtd[card->num_links];
  3121. for (i = 0; i < card->num_links; i++)
  3122. card->rtd[i].dai_link = &card->dai_link[i];
  3123. INIT_LIST_HEAD(&card->list);
  3124. INIT_LIST_HEAD(&card->dapm_dirty);
  3125. card->instantiated = 0;
  3126. mutex_init(&card->mutex);
  3127. mutex_init(&card->dapm_mutex);
  3128. ret = snd_soc_instantiate_card(card);
  3129. if (ret != 0)
  3130. soc_cleanup_card_debugfs(card);
  3131. return ret;
  3132. }
  3133. EXPORT_SYMBOL_GPL(snd_soc_register_card);
  3134. /**
  3135. * snd_soc_unregister_card - Unregister a card with the ASoC core
  3136. *
  3137. * @card: Card to unregister
  3138. *
  3139. */
  3140. int snd_soc_unregister_card(struct snd_soc_card *card)
  3141. {
  3142. if (card->instantiated)
  3143. soc_cleanup_card_resources(card);
  3144. dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
  3145. return 0;
  3146. }
  3147. EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
  3148. /*
  3149. * Simplify DAI link configuration by removing ".-1" from device names
  3150. * and sanitizing names.
  3151. */
  3152. static char *fmt_single_name(struct device *dev, int *id)
  3153. {
  3154. char *found, name[NAME_SIZE];
  3155. int id1, id2;
  3156. if (dev_name(dev) == NULL)
  3157. return NULL;
  3158. strlcpy(name, dev_name(dev), NAME_SIZE);
  3159. /* are we a "%s.%d" name (platform and SPI components) */
  3160. found = strstr(name, dev->driver->name);
  3161. if (found) {
  3162. /* get ID */
  3163. if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
  3164. /* discard ID from name if ID == -1 */
  3165. if (*id == -1)
  3166. found[strlen(dev->driver->name)] = '\0';
  3167. }
  3168. } else {
  3169. /* I2C component devices are named "bus-addr" */
  3170. if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
  3171. char tmp[NAME_SIZE];
  3172. /* create unique ID number from I2C addr and bus */
  3173. *id = ((id1 & 0xffff) << 16) + id2;
  3174. /* sanitize component name for DAI link creation */
  3175. snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
  3176. strlcpy(name, tmp, NAME_SIZE);
  3177. } else
  3178. *id = 0;
  3179. }
  3180. return kstrdup(name, GFP_KERNEL);
  3181. }
  3182. /*
  3183. * Simplify DAI link naming for single devices with multiple DAIs by removing
  3184. * any ".-1" and using the DAI name (instead of device name).
  3185. */
  3186. static inline char *fmt_multiple_name(struct device *dev,
  3187. struct snd_soc_dai_driver *dai_drv)
  3188. {
  3189. if (dai_drv->name == NULL) {
  3190. dev_err(dev,
  3191. "ASoC: error - multiple DAI %s registered with no name\n",
  3192. dev_name(dev));
  3193. return NULL;
  3194. }
  3195. return kstrdup(dai_drv->name, GFP_KERNEL);
  3196. }
  3197. /**
  3198. * snd_soc_register_dai - Register a DAI with the ASoC core
  3199. *
  3200. * @dai: DAI to register
  3201. */
  3202. static int snd_soc_register_dai(struct device *dev,
  3203. struct snd_soc_dai_driver *dai_drv)
  3204. {
  3205. struct snd_soc_codec *codec;
  3206. struct snd_soc_dai *dai;
  3207. dev_dbg(dev, "ASoC: dai register %s\n", dev_name(dev));
  3208. dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
  3209. if (dai == NULL)
  3210. return -ENOMEM;
  3211. /* create DAI component name */
  3212. dai->name = fmt_single_name(dev, &dai->id);
  3213. if (dai->name == NULL) {
  3214. kfree(dai);
  3215. return -ENOMEM;
  3216. }
  3217. dai->dev = dev;
  3218. dai->driver = dai_drv;
  3219. dai->dapm.dev = dev;
  3220. if (!dai->driver->ops)
  3221. dai->driver->ops = &null_dai_ops;
  3222. mutex_lock(&client_mutex);
  3223. list_for_each_entry(codec, &codec_list, list) {
  3224. if (codec->dev == dev) {
  3225. dev_dbg(dev, "ASoC: Mapped DAI %s to CODEC %s\n",
  3226. dai->name, codec->name);
  3227. dai->codec = codec;
  3228. break;
  3229. }
  3230. }
  3231. if (!dai->codec)
  3232. dai->dapm.idle_bias_off = 1;
  3233. list_add(&dai->list, &dai_list);
  3234. mutex_unlock(&client_mutex);
  3235. dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
  3236. return 0;
  3237. }
  3238. /**
  3239. * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
  3240. *
  3241. * @dai: DAI to unregister
  3242. */
  3243. static void snd_soc_unregister_dai(struct device *dev)
  3244. {
  3245. struct snd_soc_dai *dai;
  3246. list_for_each_entry(dai, &dai_list, list) {
  3247. if (dev == dai->dev)
  3248. goto found;
  3249. }
  3250. return;
  3251. found:
  3252. mutex_lock(&client_mutex);
  3253. list_del(&dai->list);
  3254. mutex_unlock(&client_mutex);
  3255. dev_dbg(dev, "ASoC: Unregistered DAI '%s'\n", dai->name);
  3256. kfree(dai->name);
  3257. kfree(dai);
  3258. }
  3259. /**
  3260. * snd_soc_register_dais - Register multiple DAIs with the ASoC core
  3261. *
  3262. * @dai: Array of DAIs to register
  3263. * @count: Number of DAIs
  3264. */
  3265. static int snd_soc_register_dais(struct device *dev,
  3266. struct snd_soc_dai_driver *dai_drv, size_t count)
  3267. {
  3268. struct snd_soc_codec *codec;
  3269. struct snd_soc_dai *dai;
  3270. int i, ret = 0;
  3271. dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count);
  3272. for (i = 0; i < count; i++) {
  3273. dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
  3274. if (dai == NULL) {
  3275. ret = -ENOMEM;
  3276. goto err;
  3277. }
  3278. /* create DAI component name */
  3279. dai->name = fmt_multiple_name(dev, &dai_drv[i]);
  3280. if (dai->name == NULL) {
  3281. kfree(dai);
  3282. ret = -EINVAL;
  3283. goto err;
  3284. }
  3285. dai->dev = dev;
  3286. dai->driver = &dai_drv[i];
  3287. if (dai->driver->id)
  3288. dai->id = dai->driver->id;
  3289. else
  3290. dai->id = i;
  3291. dai->dapm.dev = dev;
  3292. if (!dai->driver->ops)
  3293. dai->driver->ops = &null_dai_ops;
  3294. mutex_lock(&client_mutex);
  3295. list_for_each_entry(codec, &codec_list, list) {
  3296. if (codec->dev == dev) {
  3297. dev_dbg(dev,
  3298. "ASoC: Mapped DAI %s to CODEC %s\n",
  3299. dai->name, codec->name);
  3300. dai->codec = codec;
  3301. break;
  3302. }
  3303. }
  3304. if (!dai->codec)
  3305. dai->dapm.idle_bias_off = 1;
  3306. list_add(&dai->list, &dai_list);
  3307. mutex_unlock(&client_mutex);
  3308. dev_dbg(dai->dev, "ASoC: Registered DAI '%s'\n", dai->name);
  3309. }
  3310. return 0;
  3311. err:
  3312. for (i--; i >= 0; i--)
  3313. snd_soc_unregister_dai(dev);
  3314. return ret;
  3315. }
  3316. /**
  3317. * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
  3318. *
  3319. * @dai: Array of DAIs to unregister
  3320. * @count: Number of DAIs
  3321. */
  3322. static void snd_soc_unregister_dais(struct device *dev, size_t count)
  3323. {
  3324. int i;
  3325. for (i = 0; i < count; i++)
  3326. snd_soc_unregister_dai(dev);
  3327. }
  3328. /**
  3329. * snd_soc_add_platform - Add a platform to the ASoC core
  3330. * @dev: The parent device for the platform
  3331. * @platform: The platform to add
  3332. * @platform_driver: The driver for the platform
  3333. */
  3334. int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
  3335. const struct snd_soc_platform_driver *platform_drv)
  3336. {
  3337. /* create platform component name */
  3338. platform->name = fmt_single_name(dev, &platform->id);
  3339. if (platform->name == NULL) {
  3340. kfree(platform);
  3341. return -ENOMEM;
  3342. }
  3343. platform->dev = dev;
  3344. platform->driver = platform_drv;
  3345. platform->dapm.dev = dev;
  3346. platform->dapm.platform = platform;
  3347. platform->dapm.stream_event = platform_drv->stream_event;
  3348. mutex_init(&platform->mutex);
  3349. mutex_lock(&client_mutex);
  3350. list_add(&platform->list, &platform_list);
  3351. mutex_unlock(&client_mutex);
  3352. dev_dbg(dev, "ASoC: Registered platform '%s'\n", platform->name);
  3353. return 0;
  3354. }
  3355. EXPORT_SYMBOL_GPL(snd_soc_add_platform);
  3356. /**
  3357. * snd_soc_register_platform - Register a platform with the ASoC core
  3358. *
  3359. * @platform: platform to register
  3360. */
  3361. int snd_soc_register_platform(struct device *dev,
  3362. const struct snd_soc_platform_driver *platform_drv)
  3363. {
  3364. struct snd_soc_platform *platform;
  3365. int ret;
  3366. dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
  3367. platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
  3368. if (platform == NULL)
  3369. return -ENOMEM;
  3370. ret = snd_soc_add_platform(dev, platform, platform_drv);
  3371. if (ret)
  3372. kfree(platform);
  3373. return ret;
  3374. }
  3375. EXPORT_SYMBOL_GPL(snd_soc_register_platform);
  3376. /**
  3377. * snd_soc_remove_platform - Remove a platform from the ASoC core
  3378. * @platform: the platform to remove
  3379. */
  3380. void snd_soc_remove_platform(struct snd_soc_platform *platform)
  3381. {
  3382. mutex_lock(&client_mutex);
  3383. list_del(&platform->list);
  3384. mutex_unlock(&client_mutex);
  3385. dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
  3386. platform->name);
  3387. kfree(platform->name);
  3388. }
  3389. EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
  3390. struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
  3391. {
  3392. struct snd_soc_platform *platform;
  3393. list_for_each_entry(platform, &platform_list, list) {
  3394. if (dev == platform->dev)
  3395. return platform;
  3396. }
  3397. return NULL;
  3398. }
  3399. EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
  3400. /**
  3401. * snd_soc_unregister_platform - Unregister a platform from the ASoC core
  3402. *
  3403. * @platform: platform to unregister
  3404. */
  3405. void snd_soc_unregister_platform(struct device *dev)
  3406. {
  3407. struct snd_soc_platform *platform;
  3408. platform = snd_soc_lookup_platform(dev);
  3409. if (!platform)
  3410. return;
  3411. snd_soc_remove_platform(platform);
  3412. kfree(platform);
  3413. }
  3414. EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
  3415. static u64 codec_format_map[] = {
  3416. SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
  3417. SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
  3418. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
  3419. SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
  3420. SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
  3421. SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
  3422. SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
  3423. SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
  3424. SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
  3425. SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
  3426. SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
  3427. SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
  3428. SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
  3429. SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
  3430. SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
  3431. | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
  3432. };
  3433. /* Fix up the DAI formats for endianness: codecs don't actually see
  3434. * the endianness of the data but we're using the CPU format
  3435. * definitions which do need to include endianness so we ensure that
  3436. * codec DAIs always have both big and little endian variants set.
  3437. */
  3438. static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
  3439. {
  3440. int i;
  3441. for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
  3442. if (stream->formats & codec_format_map[i])
  3443. stream->formats |= codec_format_map[i];
  3444. }
  3445. /**
  3446. * snd_soc_register_codec - Register a codec with the ASoC core
  3447. *
  3448. * @codec: codec to register
  3449. */
  3450. int snd_soc_register_codec(struct device *dev,
  3451. const struct snd_soc_codec_driver *codec_drv,
  3452. struct snd_soc_dai_driver *dai_drv,
  3453. int num_dai)
  3454. {
  3455. size_t reg_size;
  3456. struct snd_soc_codec *codec;
  3457. int ret, i;
  3458. dev_dbg(dev, "codec register %s\n", dev_name(dev));
  3459. codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
  3460. if (codec == NULL)
  3461. return -ENOMEM;
  3462. /* create CODEC component name */
  3463. codec->name = fmt_single_name(dev, &codec->id);
  3464. if (codec->name == NULL) {
  3465. ret = -ENOMEM;
  3466. goto fail_codec;
  3467. }
  3468. if (codec_drv->compress_type)
  3469. codec->compress_type = codec_drv->compress_type;
  3470. else
  3471. codec->compress_type = SND_SOC_FLAT_COMPRESSION;
  3472. codec->write = codec_drv->write;
  3473. codec->read = codec_drv->read;
  3474. codec->volatile_register = codec_drv->volatile_register;
  3475. codec->readable_register = codec_drv->readable_register;
  3476. codec->writable_register = codec_drv->writable_register;
  3477. codec->ignore_pmdown_time = codec_drv->ignore_pmdown_time;
  3478. codec->dapm.bias_level = SND_SOC_BIAS_OFF;
  3479. codec->dapm.dev = dev;
  3480. codec->dapm.codec = codec;
  3481. codec->dapm.seq_notifier = codec_drv->seq_notifier;
  3482. codec->dapm.stream_event = codec_drv->stream_event;
  3483. codec->dev = dev;
  3484. codec->driver = codec_drv;
  3485. codec->num_dai = num_dai;
  3486. mutex_init(&codec->mutex);
  3487. /* allocate CODEC register cache */
  3488. if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
  3489. reg_size = codec_drv->reg_cache_size * codec_drv->reg_word_size;
  3490. codec->reg_size = reg_size;
  3491. /* it is necessary to make a copy of the default register cache
  3492. * because in the case of using a compression type that requires
  3493. * the default register cache to be marked as the
  3494. * kernel might have freed the array by the time we initialize
  3495. * the cache.
  3496. */
  3497. if (codec_drv->reg_cache_default) {
  3498. codec->reg_def_copy = kmemdup(codec_drv->reg_cache_default,
  3499. reg_size, GFP_KERNEL);
  3500. if (!codec->reg_def_copy) {
  3501. ret = -ENOMEM;
  3502. goto fail_codec_name;
  3503. }
  3504. }
  3505. }
  3506. if (codec_drv->reg_access_size && codec_drv->reg_access_default) {
  3507. if (!codec->volatile_register)
  3508. codec->volatile_register = snd_soc_default_volatile_register;
  3509. if (!codec->readable_register)
  3510. codec->readable_register = snd_soc_default_readable_register;
  3511. if (!codec->writable_register)
  3512. codec->writable_register = snd_soc_default_writable_register;
  3513. }
  3514. for (i = 0; i < num_dai; i++) {
  3515. fixup_codec_formats(&dai_drv[i].playback);
  3516. fixup_codec_formats(&dai_drv[i].capture);
  3517. }
  3518. mutex_lock(&client_mutex);
  3519. list_add(&codec->list, &codec_list);
  3520. mutex_unlock(&client_mutex);
  3521. /* register any DAIs */
  3522. ret = snd_soc_register_dais(dev, dai_drv, num_dai);
  3523. if (ret < 0) {
  3524. dev_err(codec->dev, "ASoC: Failed to regster DAIs: %d\n", ret);
  3525. goto fail_codec_name;
  3526. }
  3527. dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n", codec->name);
  3528. return 0;
  3529. fail_codec_name:
  3530. mutex_lock(&client_mutex);
  3531. list_del(&codec->list);
  3532. mutex_unlock(&client_mutex);
  3533. kfree(codec->name);
  3534. fail_codec:
  3535. kfree(codec);
  3536. return ret;
  3537. }
  3538. EXPORT_SYMBOL_GPL(snd_soc_register_codec);
  3539. /**
  3540. * snd_soc_unregister_codec - Unregister a codec from the ASoC core
  3541. *
  3542. * @codec: codec to unregister
  3543. */
  3544. void snd_soc_unregister_codec(struct device *dev)
  3545. {
  3546. struct snd_soc_codec *codec;
  3547. list_for_each_entry(codec, &codec_list, list) {
  3548. if (dev == codec->dev)
  3549. goto found;
  3550. }
  3551. return;
  3552. found:
  3553. snd_soc_unregister_dais(dev, codec->num_dai);
  3554. mutex_lock(&client_mutex);
  3555. list_del(&codec->list);
  3556. mutex_unlock(&client_mutex);
  3557. dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n", codec->name);
  3558. snd_soc_cache_exit(codec);
  3559. kfree(codec->reg_def_copy);
  3560. kfree(codec->name);
  3561. kfree(codec);
  3562. }
  3563. EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
  3564. /**
  3565. * snd_soc_register_component - Register a component with the ASoC core
  3566. *
  3567. */
  3568. int snd_soc_register_component(struct device *dev,
  3569. const struct snd_soc_component_driver *cmpnt_drv,
  3570. struct snd_soc_dai_driver *dai_drv,
  3571. int num_dai)
  3572. {
  3573. struct snd_soc_component *cmpnt;
  3574. int ret;
  3575. dev_dbg(dev, "component register %s\n", dev_name(dev));
  3576. cmpnt = devm_kzalloc(dev, sizeof(*cmpnt), GFP_KERNEL);
  3577. if (!cmpnt) {
  3578. dev_err(dev, "ASoC: Failed to allocate memory\n");
  3579. return -ENOMEM;
  3580. }
  3581. cmpnt->name = fmt_single_name(dev, &cmpnt->id);
  3582. if (!cmpnt->name) {
  3583. dev_err(dev, "ASoC: Failed to simplifying name\n");
  3584. return -ENOMEM;
  3585. }
  3586. cmpnt->dev = dev;
  3587. cmpnt->driver = cmpnt_drv;
  3588. cmpnt->num_dai = num_dai;
  3589. /*
  3590. * snd_soc_register_dai() uses fmt_single_name(), and
  3591. * snd_soc_register_dais() uses fmt_multiple_name()
  3592. * for dai->name which is used for name based matching
  3593. */
  3594. if (1 == num_dai)
  3595. ret = snd_soc_register_dai(dev, dai_drv);
  3596. else
  3597. ret = snd_soc_register_dais(dev, dai_drv, num_dai);
  3598. if (ret < 0) {
  3599. dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
  3600. goto error_component_name;
  3601. }
  3602. mutex_lock(&client_mutex);
  3603. list_add(&cmpnt->list, &component_list);
  3604. mutex_unlock(&client_mutex);
  3605. dev_dbg(cmpnt->dev, "ASoC: Registered component '%s'\n", cmpnt->name);
  3606. return ret;
  3607. error_component_name:
  3608. kfree(cmpnt->name);
  3609. return ret;
  3610. }
  3611. EXPORT_SYMBOL_GPL(snd_soc_register_component);
  3612. /**
  3613. * snd_soc_unregister_component - Unregister a component from the ASoC core
  3614. *
  3615. */
  3616. void snd_soc_unregister_component(struct device *dev)
  3617. {
  3618. struct snd_soc_component *cmpnt;
  3619. list_for_each_entry(cmpnt, &component_list, list) {
  3620. if (dev == cmpnt->dev)
  3621. goto found;
  3622. }
  3623. return;
  3624. found:
  3625. snd_soc_unregister_dais(dev, cmpnt->num_dai);
  3626. mutex_lock(&client_mutex);
  3627. list_del(&cmpnt->list);
  3628. mutex_unlock(&client_mutex);
  3629. dev_dbg(dev, "ASoC: Unregistered component '%s'\n", cmpnt->name);
  3630. kfree(cmpnt->name);
  3631. }
  3632. EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
  3633. /* Retrieve a card's name from device tree */
  3634. int snd_soc_of_parse_card_name(struct snd_soc_card *card,
  3635. const char *propname)
  3636. {
  3637. struct device_node *np = card->dev->of_node;
  3638. int ret;
  3639. ret = of_property_read_string_index(np, propname, 0, &card->name);
  3640. /*
  3641. * EINVAL means the property does not exist. This is fine providing
  3642. * card->name was previously set, which is checked later in
  3643. * snd_soc_register_card.
  3644. */
  3645. if (ret < 0 && ret != -EINVAL) {
  3646. dev_err(card->dev,
  3647. "ASoC: Property '%s' could not be read: %d\n",
  3648. propname, ret);
  3649. return ret;
  3650. }
  3651. return 0;
  3652. }
  3653. EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
  3654. int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
  3655. const char *propname)
  3656. {
  3657. struct device_node *np = card->dev->of_node;
  3658. int num_routes;
  3659. struct snd_soc_dapm_route *routes;
  3660. int i, ret;
  3661. num_routes = of_property_count_strings(np, propname);
  3662. if (num_routes < 0 || num_routes & 1) {
  3663. dev_err(card->dev,
  3664. "ASoC: Property '%s' does not exist or its length is not even\n",
  3665. propname);
  3666. return -EINVAL;
  3667. }
  3668. num_routes /= 2;
  3669. if (!num_routes) {
  3670. dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
  3671. propname);
  3672. return -EINVAL;
  3673. }
  3674. routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
  3675. GFP_KERNEL);
  3676. if (!routes) {
  3677. dev_err(card->dev,
  3678. "ASoC: Could not allocate DAPM route table\n");
  3679. return -EINVAL;
  3680. }
  3681. for (i = 0; i < num_routes; i++) {
  3682. ret = of_property_read_string_index(np, propname,
  3683. 2 * i, &routes[i].sink);
  3684. if (ret) {
  3685. dev_err(card->dev,
  3686. "ASoC: Property '%s' index %d could not be read: %d\n",
  3687. propname, 2 * i, ret);
  3688. return -EINVAL;
  3689. }
  3690. ret = of_property_read_string_index(np, propname,
  3691. (2 * i) + 1, &routes[i].source);
  3692. if (ret) {
  3693. dev_err(card->dev,
  3694. "ASoC: Property '%s' index %d could not be read: %d\n",
  3695. propname, (2 * i) + 1, ret);
  3696. return -EINVAL;
  3697. }
  3698. }
  3699. card->num_dapm_routes = num_routes;
  3700. card->dapm_routes = routes;
  3701. return 0;
  3702. }
  3703. EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
  3704. unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
  3705. const char *prefix)
  3706. {
  3707. int ret, i;
  3708. char prop[128];
  3709. unsigned int format = 0;
  3710. int bit, frame;
  3711. const char *str;
  3712. struct {
  3713. char *name;
  3714. unsigned int val;
  3715. } of_fmt_table[] = {
  3716. { "i2s", SND_SOC_DAIFMT_I2S },
  3717. { "right_j", SND_SOC_DAIFMT_RIGHT_J },
  3718. { "left_j", SND_SOC_DAIFMT_LEFT_J },
  3719. { "dsp_a", SND_SOC_DAIFMT_DSP_A },
  3720. { "dsp_b", SND_SOC_DAIFMT_DSP_B },
  3721. { "ac97", SND_SOC_DAIFMT_AC97 },
  3722. { "pdm", SND_SOC_DAIFMT_PDM},
  3723. { "msb", SND_SOC_DAIFMT_MSB },
  3724. { "lsb", SND_SOC_DAIFMT_LSB },
  3725. };
  3726. if (!prefix)
  3727. prefix = "";
  3728. /*
  3729. * check "[prefix]format = xxx"
  3730. * SND_SOC_DAIFMT_FORMAT_MASK area
  3731. */
  3732. snprintf(prop, sizeof(prop), "%sformat", prefix);
  3733. ret = of_property_read_string(np, prop, &str);
  3734. if (ret == 0) {
  3735. for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
  3736. if (strcmp(str, of_fmt_table[i].name) == 0) {
  3737. format |= of_fmt_table[i].val;
  3738. break;
  3739. }
  3740. }
  3741. }
  3742. /*
  3743. * check "[prefix]continuous-clock"
  3744. * SND_SOC_DAIFMT_CLOCK_MASK area
  3745. */
  3746. snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
  3747. if (of_get_property(np, prop, NULL))
  3748. format |= SND_SOC_DAIFMT_CONT;
  3749. else
  3750. format |= SND_SOC_DAIFMT_GATED;
  3751. /*
  3752. * check "[prefix]bitclock-inversion"
  3753. * check "[prefix]frame-inversion"
  3754. * SND_SOC_DAIFMT_INV_MASK area
  3755. */
  3756. snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
  3757. bit = !!of_get_property(np, prop, NULL);
  3758. snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
  3759. frame = !!of_get_property(np, prop, NULL);
  3760. switch ((bit << 4) + frame) {
  3761. case 0x11:
  3762. format |= SND_SOC_DAIFMT_IB_IF;
  3763. break;
  3764. case 0x10:
  3765. format |= SND_SOC_DAIFMT_IB_NF;
  3766. break;
  3767. case 0x01:
  3768. format |= SND_SOC_DAIFMT_NB_IF;
  3769. break;
  3770. default:
  3771. /* SND_SOC_DAIFMT_NB_NF is default */
  3772. break;
  3773. }
  3774. /*
  3775. * check "[prefix]bitclock-master"
  3776. * check "[prefix]frame-master"
  3777. * SND_SOC_DAIFMT_MASTER_MASK area
  3778. */
  3779. snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
  3780. bit = !!of_get_property(np, prop, NULL);
  3781. snprintf(prop, sizeof(prop), "%sframe-master", prefix);
  3782. frame = !!of_get_property(np, prop, NULL);
  3783. switch ((bit << 4) + frame) {
  3784. case 0x11:
  3785. format |= SND_SOC_DAIFMT_CBM_CFM;
  3786. break;
  3787. case 0x10:
  3788. format |= SND_SOC_DAIFMT_CBM_CFS;
  3789. break;
  3790. case 0x01:
  3791. format |= SND_SOC_DAIFMT_CBS_CFM;
  3792. break;
  3793. default:
  3794. format |= SND_SOC_DAIFMT_CBS_CFS;
  3795. break;
  3796. }
  3797. return format;
  3798. }
  3799. EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
  3800. static int __init snd_soc_init(void)
  3801. {
  3802. #ifdef CONFIG_DEBUG_FS
  3803. snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
  3804. if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
  3805. pr_warn("ASoC: Failed to create debugfs directory\n");
  3806. snd_soc_debugfs_root = NULL;
  3807. }
  3808. if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
  3809. &codec_list_fops))
  3810. pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
  3811. if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
  3812. &dai_list_fops))
  3813. pr_warn("ASoC: Failed to create DAI list debugfs file\n");
  3814. if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
  3815. &platform_list_fops))
  3816. pr_warn("ASoC: Failed to create platform list debugfs file\n");
  3817. #endif
  3818. snd_soc_util_init();
  3819. return platform_driver_register(&soc_driver);
  3820. }
  3821. module_init(snd_soc_init);
  3822. static void __exit snd_soc_exit(void)
  3823. {
  3824. snd_soc_util_exit();
  3825. #ifdef CONFIG_DEBUG_FS
  3826. debugfs_remove_recursive(snd_soc_debugfs_root);
  3827. #endif
  3828. platform_driver_unregister(&soc_driver);
  3829. }
  3830. module_exit(snd_soc_exit);
  3831. /* Module information */
  3832. MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
  3833. MODULE_DESCRIPTION("ALSA SoC Core");
  3834. MODULE_LICENSE("GPL");
  3835. MODULE_ALIAS("platform:soc-audio");