soc-core.c 98 KB

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