soc-core.c 97 KB

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