soc-core.c 117 KB

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