soc-core.c 98 KB

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