soc-core.c 97 KB

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