soc-core.c 121 KB

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