soc-core.c 107 KB

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