soc-core.c 115 KB

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