soc-core.c 107 KB

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