soc-core.c 114 KB

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