soc-core.c 78 KB

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