soc-core.c 113 KB

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