soc-core.c 116 KB

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