soc-core.c 94 KB

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