soc-core.c 106 KB

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