soc-core.c 96 KB

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