soc-core.c 87 KB

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